如何 运行 在多个行大小可变的数据帧上运行,然后生成一个仅包含函数结果的新数据帧

How to run function on multiple dataframes of variable row sizes, then generate a new dataframe with just the function results

我有一个文件夹,里面装满了列数相等但行数可变的 CSV。我想将每一个都转换为一个数据框和 运行 它们的一个简单函数,并创建一个仅使用函数值和文件名作为索引的新数据框。

到目前为止我有:

import os.path
import tkinter.filedialog as filedialog
import glob
import pandas as pd

file_path = filedialog.askdirectory()
pattern = os.path.join(file_path, '*.csv')
files = glob.glob(pattern)

for index, file in enumerate(files):
    df = pd.read_csv(file, sep=',', index_col=[0])
    df.loc['total'] = df.sum(numeric_only=True, axis=0) # or any function
    pd.concat(df2[df.index == 'total'])

df.to_csv('file_path')

我敢肯定有几种方法会把它搞砸,但我们不胜感激任何建议

import os.path
import tkinter.filedialog as filedialog
import glob
import pandas as pd

file_path = filedialog.askdirectory()
pattern = os.path.join(file_path, '*.csv')
files = glob.glob(pattern)

dfs = []
for index, file in enumerate(files):
    df = pd.read_csv(file, sep=',', index_col=[0])
    # Would remove the .loc, but it does no harm
    df.loc['total'] = df.sum(numeric_only=True, axis=0) # or any function  
    dfs.append(df[['total']])

df_total = pd.concat(dfs).reset_index(drop=True)
df_total.to_csv('file_path')

好的,我明白了:

import os.path
import tkinter.filedialog as filedialog
import glob
import pandas as pd

file_path = filedialog.askdirectory()
pattern = os.path.join(file_path, '*.csv')
files = glob.glob(pattern)

filename = pd.DataFrame(columns=['Filename'])
filename['Filename'] = pd.Series([file for file in files]).reset_index(drop=True)

dfs = []
for index, file in enumerate(files):
    df = pd.read_csv(file, sep=',', index_col=[0])
    # Would remove the .loc, but it does no harm
    df.loc['total'] = df.sum(numeric_only=True, axis=0) # or any function  
    dfs.append(df)

dfs = pd.concat(dfs)
total = dfs[dfs.index == 'total'][['dfcolumn1','dfcolumn2',etc]]#write column names exactly as they appear on csv
total_named = filename.join(total.set_index(filename.index))
total_named.to_csv('file_path')