gpt4 book ai didi

python - 如何字符串连接 Pandas 中的多个字符串列?

转载 作者:太空宇宙 更新时间:2023-11-04 00:02:44 28 4
gpt4 key购买 nike

我想将 DataFrame 中的两行连接成一行。我当前的代码:

import pandas as 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 .

最佳答案

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

关于python - 如何字符串连接 Pandas 中的多个字符串列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55183649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com