合并pandas中的行

时间:2016-10-05 03:56:31

标签: python pandas dataframe

假设我有一个看起来像这样的数据框

Line   VarA        VarB
345    Something   abc
346    Something2  def
347    Something   ghi
348    Something2  jkl
349    Something   mno
350    Something2  pqr
351    Something   stu
352    Something2  vwx

如何将此数据框转换为如下所示:

VarA       VarB
Something  abcghimnostu
Something2 defjklpqrvwx

1 个答案:

答案 0 :(得分:2)

您可以groupbysum

print(df.groupby('VarA')['VarB'].sum())
VarA
something     abcghimnostu
something2    defjklpqrvwx
Name: VarB, dtype: object