我有一组数据帧,我这样组合:
frames = [df1, df2, df3, df4, df5]
result = pd.concat(frames)
这给了我一个如下所示的输出:
name time thing stuff
0 a NaN X NaN
0 a 10:00 NaN NaN
0 a NaN NaN blah
1 b 11:00 NaN NaN
1 b NaN Y NaN
1 b NaN NaN hi
我想合并行,所以它看起来像这样:
name time thing stuff
0 a 10:00 X blah
1 b 11:00 Y hi
我已尝试过groupby但它似乎无法工作:
In: result = result.groupby('name')
print result
Out: result = pd.concat(frames)
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x10cc2bd10>
任何人都知道如何才能使这项工作成功?
谢谢!
答案 0 :(得分:3)
将groupby
与first
df.groupby('name',as_index=False).first()
Out[447]:
name time thing stuff
0 a 10:00 X blah
1 b 11:00 Y hi