返回

python—如何在pandas中将不同数据帧中的列汇总为单个数据帧

发布时间:2022-06-27 19:33:50 401
# node.js

样本数据:

import pandas as pd

df1 = pd.DataFrame() 
df1["Col1"] = [0,2,4,6,2] 
df1["Col2"] = [5,1,3,4,0]
df1["Col3"] = [8,0,5,1,7]
df1["Col4"] = [1,4,6,0,8]
#df1_new = df1.iloc[:, 1:3]

df2 = pd.DataFrame() 
df2["Col1"] = [8,2,4,6,2,3,5] 
df2["Col2"] = [3,7,3,4,0,6,8]
df2["Col3"] = [5,0,5,1,7,9,1]
df2["Col4"] = [0,4,6,0,8,6,0]
#df2_new = df1.iloc[:, 1:3]

dataframes = [df1, df2]

for df in dataframes:
    df_new=df.iloc[:, 1:3]
    print(df_new.sum(axis=0))

上述结果如下所示:

Col2    13  
Col3    21  
dtype: int64
Col2    31  
Col3    28  
dtype: int64

但我如何将两个数据帧相加并将其放入一个数据帧中呢?

结果应如下所示:

xlsx_files = glob.glob(os.path.join(path, "*.xlsx"))
#print(csv_files)

# loop over the list of csv files
for f in xlsx_files: 
    # create df from each excel file
    dfs = pd.read_excel(f)
    # grab file name to user it in summarized df
    file_name =  f.split("\\")[-1]
    new_df = pd.concat([dfs]).iloc[:,13:28].sum()
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(2)
按点赞数排序
用户头像
下一篇
json-新行不与AWS SES保持一致 2022-06-27 17:36:18