如何使用 python pandas 在循环中加入多个数据帧

Ser*_*dia 2 python join dataframe pandas

我在每个Excel工作表上有3个表:sheet1 - Gross,sheet2 - Margin,sheet3 -Revenue

所以我能够迭代每张工作表并将其取消旋转。

但我怎样才能将它们结合在一起呢?

在此输入图像描述

    sheet_names = ['Gross','Margin','Revenue']

    full_table = pd.DataFrame()
    for sheet in sheet_names:
        df = pd.read_excel(BudgetData.xlsx', sheet_name = sheet, index=False)
        unpvt = pd.melt(df,id_vars=['Company'], var_name ='Month', value_name = sheet)
# how can I join unpivoted dataframes here?
        print(unpvt)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

期望的结果:

在此输入图像描述

更新:

谢谢@Celius Stingher。我想这就是我所需要的。它只是给了我奇怪的排序:

在此输入图像描述

并给我这个警告:

Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

  from ipykernel import kernelapp as app
Run Code Online (Sandbox Code Playgroud)

Iva*_*sky 5

因此,您似乎正在进行旋转,但没有将每个未旋转的数据帧保存在任何地方。让我们创建一个数据帧列表,它将存储每个未旋转的数据帧。稍后,我们将将该数据帧列表作为pd.concat函数的参数传递以执行串联。

sheet_names = ['Gross','Margin','Revenue']
list_of_df = []
full_table = pd.DataFrame()
for sheet in sheet_names:
    df = pd.read_excel(BudgetData.xlsx', sheet_name = sheet, index=False)
    df = pd.melt(df,id_vars=['Company'], var_name ='Month', value_name = sheet)
    list_of_df.append(df)

full_df = pd.concat(list_of_df,ignore_index=True)
full_df = full_df.sort_values(['Company','Month'])
print(full_df)
Run Code Online (Sandbox Code Playgroud)

编辑:

现在我明白了您的需求,让我们尝试不同的方法。循环后尝试以下代码pd.concat

full_df = list_of_df[0].merge(list_of_df[1],on=['Company','Month']).merge(list_of_df[2],on=['Company','Month'])
Run Code Online (Sandbox Code Playgroud)