Home > Blockchain >  How to divide string in column separated by space to 3 new column in DataFrame in Python Pandas?
How to divide string in column separated by space to 3 new column in DataFrame in Python Pandas?

Time:10-19

I have DataFrame in Python Pandas like below (in my real DataFrame I have of course many more columns):

COL1                                                | ...  | COLn
----------------------------------------------------|------|---------
ABC_20201105 XX_6M_BEFORE_100_150 PE_T1_20201105    | ...  | ...
ZZZ_20201105 XX_6M_BEFORE_200_400 PE_P1_20201105    | ...  | ...
ZBVA_20201105 XX_6M_BEFORE_100_150 PE_Z2_20201105   | ...  | ...
...                                                 | ...  | ...

And I need to divide values from COL1 to 3 new columns. Each part of value in COL1 is separated by space " ".

So, as a result I need somethin like below:

COL1 COL2 COL3 COL4 ... COLn
ABC_20201105 XX_6M_BEFORE_100_150 PE_T1_20201105 ABC_20201105 XX_6M_BEFORE_100_150 PE_T1_20201105 ... ...
ZZZ_20201105 XX_6M_BEFORE_200_400 PE_P1_20201105 ZZZ_20201105 XX_6M_BEFORE_200_400 PE_P1_20201105 ... ...
ZBVA_20201105 XX_6M_BEFORE_100_150 PE_Z2_20201105 ZBVA_20201105 XX_6M_BEFORE_100_150 PE_Z2_20201105 ... ...
... ... ... ... ... ...

How can I do this in Python Pandas ? in output i need to have also rest of my column from input DataFrame (as was presented in needed output) :)

CodePudding user response:

Here is a simple example:

import pandas as pd
a=pd.DataFrame({'col1':['a b c']})
a['col1']=a['col1'].apply(lambda x: x.split())
a[['col2','col3','col4']]=pd.DataFrame(a.col1.tolist())
a

If you have to do it for a lot of columns you can just create some column name list and use that in stead of adding them manually. If you

  • Related