gpt4 book ai didi

python - 将字符串中的每个单词读入 pandas 新行

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:19 24 4
gpt4 key购买 nike

我有以下字符串。msg='国际货币基金组织总裁'我想要像下面这样的 Pandas 数据框['这' '管理' '导演' '的' '这' '国际货币基金组织']1 列和 6 行。

最佳答案

您可以使用 split使用 DataFrame 构造函数:

msg='The managing director of the IMF'
df = pd.DataFrame(msg.split(), columns=['col'])
print (df)
col
0 The
1 managing
2 director
3 of
4 the
5 IMF

df = pd.DataFrame([msg.split()], columns=list('abcdef'))
print (df)
a b c d e f
0 The managing director of the IMF

备选方案:

msg='The managing director of the IMF'
L = msg.split()
df = pd.DataFrame(np.array(L).reshape(-1, len(L)), columns=list('abcdef'))
print (df)
a b c d e f
0 The managing director of the IMF

关于python - 将字符串中的每个单词读入 pandas 新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43210761/

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