如何串联熊猫中的行?

时间:2019-03-15 13:24:50

标签: python dataframe

我想将DataFrame中的两行连接为一行。

代码如下:

将熊猫作为pd导入

df = pd.DataFrame(columns = ['string1', 'string2'])
df.loc[len(df), :] = ['Hello', 'This is Sam']
df.loc[len(df), :] = ['how are you?', 'from Canada']

#create the next row: ['Hello how are you?', 'This is Sam from Canada']

如何做到?

您可以测试代码here

1 个答案:

答案 0 :(得分:3)

aggappend一起使用:

df = df.append(df.agg(' '.join), ignore_index=True)
df

              string1                  string2
0               Hello              This is Sam
1        how are you?              from Canada
2  Hello how are you?  This is Sam from Canada