pandas –将功能逐行应用到DataFrame,从而产生不同尺寸的新DataFrame

时间:2019-03-13 14:41:06

标签: python pandas

我希望能够将一个函数应用于DataFrame(以行方式),以便它可以返回一个新的DataFrame,它不一定具有与原始尺寸或索引相同的尺寸。

假设我有一个DataFramedf

    A   B   C   D
0  A0  B0  C0  D0
1  A1  B1  C1  D1
2  A2  B2  C2  D2
3  A3  B3  C3  D3

和函数foo()

>>> def foo(series):
...  series['E'] = 'NEW_STUFF'
...  series['F'] = 'MORE_NEW_STUFF'
...  df = pd.DataFrame(series.drop('B')).transpose()
...  return pd.concat([df,df], keys='qw')
... 

如此

>>> foo(df.iloc[0])
      A   C   D          E               F
q 0  A0  C0  D0  NEW_STUFF  MORE_NEW_STUFF
w 0  A0  C0  D0  NEW_STUFF  MORE_NEW_STUFF

我想将foo()应用于df,以使其产生新的DataFrame,其中在每一行上运行foo()的结果都被堆叠到单个{{ 1}},有点像

DataFrame

但是,运行 A C D E F q 0 A0 C0 D0 NEW_STUFF MORE_NEW_STUFF w 0 A0 C0 D0 NEW_STUFF MORE_NEW_STUFF q 1 A1 C1 D1 NEW_STUFF MORE_NEW_STUFF w 1 A1 C1 D1 NEW_STUFF MORE_NEW_STUFF q 2 A2 C2 D2 NEW_STUFF MORE_NEW_STUFF w 2 A2 C2 D2 NEW_STUFF MORE_NEW_STUFF q 3 A3 C3 D3 NEW_STUFF MORE_NEW_STUFF w 3 A3 C3 D3 NEW_STUFF MORE_NEW_STUFF 不会返回此值。相反,我得到了

df.apply(foo, axis=1)

我需要在上面进行哪些修改才能获得所需的结果?

1 个答案:

答案 0 :(得分:4)

尝试

pd.concat([foo(y) for _,y in df.iterrows()])
Out[64]: 
      A   C   D          E               F
q 0  A0  C0  D0  NEW_STUFF  MORE_NEW_STUFF
w 0  A0  C0  D0  NEW_STUFF  MORE_NEW_STUFF
q 1  A1  C1  D1  NEW_STUFF  MORE_NEW_STUFF
w 1  A1  C1  D1  NEW_STUFF  MORE_NEW_STUFF
q 2  A2  C2  D2  NEW_STUFF  MORE_NEW_STUFF
w 2  A2  C2  D2  NEW_STUFF  MORE_NEW_STUFF
q 3  A3  C3  D3  NEW_STUFF  MORE_NEW_STUFF
w 3  A3  C3  D3  NEW_STUFF  MORE_NEW_STUFF