我想将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。
答案 0 :(得分:3)
将agg
与append
一起使用:
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