Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the list like this where items are separated by ":".

   x=['john:42:engineer',

      'michael:29:doctor']

Is there a way to change the data frame like below by using columns Name, Age, and Occupation?

    Name    Age Occupation

0   john    42  engineer

1   michael 29  doctor

1 Answer

0 votes
by (36.8k points)
edited by

You can use the below code:

df = pd.Series(x).str.split(':',expand=True)

df.columns = ['Name','Age', 'Occupation']

df

Out[172]: 

      Name Age Occupation

0     john  42   engineer

1  michael  29     doctor

If you are a beginner and want to know more about Python the do check out the Data Science with Python Course

Browse Categories

...