6

I have a dataframe as below.

My dataframe as below.

ID      list
1       a, b, c
2       a, s
3       NA
5       f, j, l

I need to break each items in the list column(String) into independent row as below:

ID      item
1       a
1       b
1       c
2       a
2       s
3       NA
5       f
5       j
5       l

Thanks.

2
  • use pandas explode with pandas str split. pandas.pydata.org/pandas-docs/stable/reference/api/… the split option converts your list column into a list, and the explode puts each item into it's own row Commented Feb 10, 2020 at 5:37
  • Thanks. list column is a string type and not list type Commented Feb 10, 2020 at 5:41

2 Answers 2

4

Use str.split to separate your items then explode:

print (df.assign(list=df["list"].str.split(", ")).explode("list"))

   ID list
0   1    a
0   1    b
0   1    c
1   2    a
1   2    s
2   3  NaN
3   5    f
3   5    j
3   5    l
Sign up to request clarification or add additional context in comments.

Comments

0

A beginners approach : Just another way of doing the same thing using pd.DataFrame.stack

df['list'] = df['list'].map(lambda x : str(x).split(','))
dfOut = pd.DataFrame(df['list'].values.tolist())
dfOut.index = df['ID']
dfOut = dfOut.stack().reset_index()
del dfOut['level_1']
dfOut.rename(columns = {0 : 'list'}, inplace = True)

Output:

   ID list
0   1    a
1   1    b
2   1    c
3   2    a
4   2    s
5   3  nan
6   5    f
7   5    j
8   5    l

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.