简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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