Splitting a column with multiple values in python

0 votes

I want to split a column in a dataframe in python. The column is like:

col_name
UO1
UO1,UO2,UO3
UO1,UO2,UO3,UO4,UO5
UO1,SO1,SO3
SO3,UO1

How do I split them as row values?

Jul 9 in Python by ana1504.k
• 7,470 points
15 views

1 answer to this question.

0 votes

Try using the following:

df['id'] = df.index+1
df.set_index('id').col_name.str.split(',', expand = True).stack().reset_index(1, drop = True).reset_index(name = 'symptoms')


    id  symptoms
0   1   UO1
1   2   UO1
2   2   UO2
3   2   UO3
4   3   UO1
5   3   UO2
6   3   UO3
7   3   UO4
8   3   UO5
9   4   UO1
10  4   SO1
11  4   SO3
12  5   SO3
13  5   UO1
answered Jul 9 by SDeb
• 12,360 points

Related Questions In Python

+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,310 points
3,942 views
+4 votes
7 answers

Splitting a List into chunks in Python

Here's a generator that yields the chunks ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
3,048 views
0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 56,160 points
601 views
0 votes
1 answer

Open multiple files using with open() in Python?

Just replace and with , and you're done: with open('a', 'w') as ...READ MORE

answered Oct 31, 2018 in Python by Priyaj
• 56,160 points
1,137 views
0 votes
1 answer

Changing Pandas data-frame to JSON type

abc.to_json(orient='records') This command will give you the desired ...READ MORE

answered May 22, 2018 in Data Analytics by anonymous
63 views
0 votes
2 answers
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,550 points
990 views
0 votes
1 answer

Python equivalent to replace function in R

There is actually a replace function in ...READ MORE

answered May 30, 2018 in Data Analytics by Bharani
• 4,550 points
59 views
0 votes
1 answer

How can I deal with python eggs for multiple platforms in one location?

Try virtualenv : http://pypi.python.org/pypi/virtualenv This helps you create isolated ...READ MORE

answered May 28 in Python by SDeb
• 12,360 points
18 views
0 votes
1 answer

Is there a string 'contains' in python?

In this case, you can use the ...READ MORE

answered Sep 25, 2018 in Python by SDeb
• 12,360 points
51 views