简体   繁体   English

如何在for循环中堆叠几个pandas DataFrame

[英]How to stack several pandas DataFrames in a for loop

Since I have multiple pandas DataFrames, I want to run the .stack() method on all of them using a for loop. 由于我有多个pandas DataFrame,因此我想使用for循环在所有pandas上运行.stack()方法。 Other methods like labeling columns and setting indexes work, but for some reason the stack method doesn't lead to any changes: 其他方法(如标记列和设置索引)也可以使用,但是由于某些原因,堆栈方法不会导致任何更改:

for df in [df1, df2, df3, df4]:
    df = df.stack()

Result: 结果:

print(df1.head())

        Q1 1990  Q2 1990  Q3 1990   ...     Q2 2018  Q3 2018  Q4 2018
EC                                  ...                              
C13840      NaN      NaN      NaN   ...         NaN      NaN      NaN
C28525      NaN      NaN      NaN   ...     8480.00  8125.00      NaN
C06541      NaN      NaN      NaN   ...         NaN      NaN      NaN
C51345      NaN      NaN      NaN   ...       13.75    15.00      NaN
C44265      NaN      NaN      NaN   ...      141.90   129.54   133.44

Expected result: 预期结果:

print(df1.head(10))

EC             
C13840  Q1 1990   NaN
        Q2 1990   NaN
        Q3 1990   NaN
        Q4 1990   NaN
        Q1 1991   NaN
        Q2 1991   NaN
        Q3 1991   NaN
        Q4 1991   NaN
        Q1 1992   NaN
        Q2 1992   NaN
        ...
        ...

Thank you. 谢谢。

Assign output to new list od Series , because stack not working inplace: 将输出分配给新列表od Series ,因为stack不能就地工作:

dfs = [df.stack() for df in [df1, df2, df3, df4]]

And then if need assign back: 然后如果需要分配:

df1, df2, df3, df4 = dfs

Or join together: 或一起加入:

df = pd.concat(dfs, axis=1)

If I understand correctly you want to modify your dfs in place however stack is not an inplace operator but returns a new df as the output 如果我理解正确的话,你要修改dfs到位然而stack是不是就地操作,但返回一个新的df作为输出

df = pd.DataFrame({'a': [1,2], 'b': [3,4]})
df1 = pd.DataFrame({'a': [5,6], 'b': [7,8]})


new_dfs = list(map(pd.DataFrame.stack, [df, df1]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM