1

在读入 18 个 CSV 文件并将它们全部附加到列表后,这样(显示前两个):

In [175] li

Out[175]: 
[   ABC_IncomeStatement_Annual_As_Originally_Reported  ...           TTM
 0                                       Gross Profit  ...   903,400,000
 1                                      Total Revenue  ...   903,400,000
 2                                   Business Revenue  ...   902,700,000
 3                                      Other Revenue  ...           NaN
 4                          Operating Income/Expenses  ...  -280,200,000
 ..                                               ...  ...           ...
 57                                         Basic EPS  ...          2.56
 58                                       Diluted EPS  ...          2.56
 59                                        Basic WASO  ...   193,576,187
 60                                      Diluted WASO  ...   193,576,187
 61                  Fiscal year ends in Jun 30 | AUD  ...           NaN
 
 [62 rows x 12 columns],
    DEF_IncomeStatement_Annual_As_Originally_Reported  ...            TTM
 0                                       Gross Profit  ...  1,321,800,000
 1                                      Total Revenue  ...  1,347,600,000
 2                                   Business Revenue  ...  1,347,600,000
 3                                      Other Revenue  ...            NaN
 4                                    Cost of Revenue  ...    -25,800,000
 ..                                               ...  ...            ...
 63                                         Basic EPS  ...           0.07
 64                                       Diluted EPS  ...           0.07
 65                                        Basic WASO  ...  2,316,707,932
 66                                      Diluted WASO  ...  2,316,707,932
 67                  Fiscal year ends in Dec 31 | AUD  ...            NaN
 

其中 len(li) = 18。然后我使用了以下代码列表:

tickers = []
for code in range(0,len(li)):
    tickers.append(li[code].columns[0][:3])

['ABC',
 'DEF', 
  etc.]

test_tuple = list(zip(tickers,li))

def tuple_to_dict(tup,di):
for a,b in tup:
    di.setdefault(a,[]).append(b)
return di

然后我创建了一个元组列表

di = {}
ASX = tuple_to_dict(test_tuple,di)

现在,当我调用时,ASX['ABC']我返回了相应的数据,但是它出现在列表对象中,而不是 DataFrame 中,它最初位于并且我希望保留它。

有没有办法维护 DataFrame 结构?已经提出了类似的问题,但没有一个与 DataFrames 列表相关。

初读如下:

import pandas as pd
import glob

path = '/Users/.../.../.../.../...'

all_files = glob.glob(path + "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename,index_col=None,header=0)
li.append(df)

非常感谢!

4

1 回答 1

1

我不明白您为什么将其转换为元组然后转换为字典。您可以像下面的代码一样将它附加到 dict :

import pandas as pd

li = [pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']}),
pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']})]
keys = ["df1", "df2"]

d = {}
for i, k in enumerate(keys):
    d[k] = li[i]

哪里print(type(d["df1"]))返回:<class 'pandas.core.frame.DataFrame'>

于 2020-08-20T10:43:51.217 回答