Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pyhon中将列表从循环迭代转换为df_Python_Pandas - Fatal编程技术网

Python 在pyhon中将列表从循环迭代转换为df

Python 在pyhon中将列表从循环迭代转换为df,python,pandas,Python,Pandas,我有一些文件,我在所有文件上运行循环,并做一些计算。我希望获得一个新的df,其中包含行侧的文件名以及正确行中每个文件的计算值 代码是: results = [] file_name = '{}' for file in folder: df = pd.read_csv(file_name.format(file)) print("reading file ", file) results.append(df['old_calc'])#this is the data i

我有一些文件,我在所有文件上运行循环,并做一些计算。我希望获得一个新的df,其中包含行侧的文件名以及正确行中每个文件的计算值

代码是:

results = []
file_name = '{}'
for file in folder:
    df = pd.read_csv(file_name.format(file))
    print("reading  file ", file)
    results.append(df['old_calc'])#this is the data i want to save to the new df and I need it .sum()
上面的代码没有按预期工作,因为它给了我:

     old calc  old calc  old calc  old calc  old calc  old calc  old calc
4           0.0         0.0         0.0         0.0         0.0         0.0   
5           0.0         0.0         0.0        59.0         0.0         0.0   
6           0.0         0.0        58.4         0.0         0.0         0.0   
7           0.0         0.0         8.4       -79.1         0.0         0.0   
8           0.0         0.0       120.9         0.0         0.0         0.0   
预期结果将是一个新的df命名结果:

file1  0
file2  0
file3  187.7
file4  20.1
file5  0

感谢您的帮助

这是提取所需数据的一种方法:

dfs = {file: pd.read_csv(file) for file in folder}
result_dict = {k: v['old_calc'].sum() for k, v in dfs.items()}

result_df = pd.DataFrame.from_dict(result_dict, orient='index')

你能包括一个数据框架的样本吗?我想应该是df['old_calc'].sum(),你正在做的是.append(df['old_calc']).sum()@ManishSaraswat,正确。我更改了它,但是我得到了一个结果列表[0,0187.7,20,1,0],而不是列为1的df。文件2。旧的calc
dfs
生成文件名->数据帧的字典
result\u dict
是文件名->总和的字典
result\u df
从字典中生成一个数据帧。我仍然不知道如何在我的代码中应用它。很抱歉dfs是我已经拥有的df。我不明白v在这里的用法