0

I have a dataframe like this

Input

>>> df

 sent      cite 
 a         1,2,3
 b         2,4
 c         5

I want to transpose the dataframe that each row contains the values of cite and sent combined. Notice that the cite and the sent can be repeated (e.g, cite 2)

Expected Output

>>> df

 cite      sent 
 1         a
 2         a
 2         b
 3         a
 4         b
 5         c

I have tried with this but it did not work

df = df.pivot( columns='cite', values='sent')
1
  • what is the dtype that is holding the values in the DataFrame? list? Commented Dec 8, 2020 at 14:53

2 Answers 2

1

Use Series.str.split with df.explode and df.sort_values:

In [141]: df = df.assign(cite=df['cite'].str.split(',')).explode('cite').sort_values('cite')

In [142]: df
Out[142]: 
  sent cite
0    a    1
0    a    2
1    b    2
0    a    3
1    b    4
2    c    5
Sign up to request clarification or add additional context in comments.

Comments

0

Try split then explode

df = df.assign(cite=df['cite'].str.split(',')).explode('cite')

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.