5

I have below format of dataframe

student   marks
a         [12,12,34]
b         [34,35]
c         [23,45,23]

i want it to convert to like below

student marks_1  marks_2 marks_3
a       12        12      34
b       34        35      Nan
c       23        45      23

how to achieve this ? any help please

2 Answers 2

5

Use join new DataFrame created by extracted column marks by pop, convert to lists and use rename for columns by custom lambda function:

f = lambda x: 'marks_{}'.format(x + 1)
df = df.join(pd.DataFrame(df.pop('marks').values.tolist()).rename(columns=f))
print (df)
  student  marks_1  marks_2  marks_3
0       a       12       12     34.0
1       b       34       35      NaN
2       c       23       45     23.0

Detail:

print (pd.DataFrame(df.pop('marks').values.tolist()))
    0   1     2
0  12  12  34.0
1  34  35   NaN
2  23  45  23.0
Sign up to request clarification or add additional context in comments.

1 Comment

As far as I can tell, the important step here is new_df = pd.DataFrame(df['marks'].tolist()). The pop call just deletes the old column after you use it, and I think you can skip the .values step. Thanks for this answer!
1

Try

dfr = pd.concat([df.student, df.marks.apply(lambda el: pd.Series(
    el, index=['marks_{}'.format(i + 1) for i in range(len(el))]))], axis=1)

The code above create an index from each element in marks then concatenate the result with the student column,

The output:

dfr

  student  marks 1  marks 2  marks 3
0       a     12.0     12.0     34.0
1       b     34.0     35.0      NaN
2       c     23.0     45.0     23.0

1 Comment

Hmmm, I think converting to Series is really slow - check this

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.