简体   繁体   English

拆分数据框中的列

[英]Split columns in dataframe

I'm cleaning up an Excel file, but in a row there is a column with a long answer and I will like to split it into multiple columns我正在清理一个 Excel 文件,但在一行中有一列答案很长,我想将其拆分为多列

I'm using this code:我正在使用此代码:

new = df["¿Qué productos sueles adquirir para hidratarse?"]= df["¿Qué productos sueles adquirir para hidratarse?"].str.split(" ", n = 2 , expand = True)

But when I print out the dataframe there is just one column and not the others, how could I make it?但是当我打印出数据框时,只有一列而不是其他列,我怎么能做到呢?

>>> df = pd.DataFrame({'test':['One Two Three']})
>>> df[1], df[2], df[3] = df['test'].str.split(' ',n=2,expand=True)
>>> df
            test  1  2  3
0  One Two Three  0  1  2

OR或者

>>> df = pd.DataFrame({'test':['One Two Three']})
>>> df = df['test'].str.split(' ',n=2,expand=True)
>>> df
     0    1      2
0  One  Two  Three

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM