如何为每一列创建汇总表?

2024-10-16 17:18:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有熊猫数据帧,每个数据帧大约有100列。我必须为所有这些列创建一个汇总表。在summary Dataframe中,我希望有一个名称(每个数据帧中都有一个,我做得很好),并将每个列的平均值和标准值放入其中

所以我的最后一张桌子的形状应该是:nxm 其中n是文件数 m是列数x2(平均值和标准值)

像这样的

name    mean_col1   std_col1    mean_col2   std_col2 
ABC        22.815293    0.103567    90.277533   0.333333
DCE        22.193991    0.12389     87.17391    0.123457

我试着跟随,但我没有得到我想要的:

list_with_names = []
list_for_mean_and_std = []

for file in glob.glob("/data/path/*.csv"):
    df = pd.read_csv(file)
    
    output = {'name':df['name'][0]}
    
    list_with_names.append(output)
    
    numerical_cols = df.select_dtypes('float64')
    
    for column in numerical_cols:
        mean_col = numerical_cols[column].mean()
        std_col = numerical_cols[column].std()
        
        output_2 = {'mean': mean_col,
                    'std': std_col}
        
        list_for_mean_and_std.append(output_2)
        
    
summary = pd.DataFrame(list_with_names, list_for_mean_and_std)

我得到一个错误Shape of passed values is (183, 1), indices imply (7874, 1),因为我用means和std以错误的方式赋值,但我不知道如何赋值

我很乐意得到关于如何改变它的任何建议