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

Ash*_*ade 1 python dataframe pandas

我有下面的字符串.msg ='国际货币基金组织的总经理'我想要像以下那样的熊猫数据框[''管理''导演''''''IMF'] 1列和6行.

jez*_*ael 6

您可以使用splitDataFrame构造函数:

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
Run Code Online (Sandbox Code Playgroud)
df = pd.DataFrame([msg.split()], columns=list('abcdef'))
print (df)
     a         b         c   d    e    f
0  The  managing  director  of  the  IMF
Run Code Online (Sandbox Code Playgroud)

替代方案:

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
Run Code Online (Sandbox Code Playgroud)