user11669928
user11669928

Reputation:

How to Split a column in dataframe based on a value?

Have a df with one column :

   0
Test  : bat
Test2 : ball
Test3 : Moon

How to split this into another column. Based on (:)

expected output :

0     |   1
Test     bat
Test2    ball
Test3    Moon

Upvotes: 0

Views: 73

Answers (1)

user459872
user459872

Reputation: 24994

You can use pandas.Series.str.split method with expand=True to split strings around given separator/delimiter. When expand=True, split will return DataFrame/MultiIndex expanding dimensionality.

>>> df
     0
0   val1:val2
1   val3:val4

>>> df["0"].str.split(':', expand=True)

    0     1
0   val1  val2
1   val3  val4

Upvotes: 1

Related Questions