0

I'm trying to transform a dataframe similar to the one below:

 index apple
 1      [(red,3),(green,2)]
 1      [(red,3)]
 1      [(yellow,9),(red,3)]
 1      [(green,2),(yellow,9)]
 1      [(green,2),(yellow,9), (pink,50)]
 2      [(yellow,14),(red,1)]
 2      [(green,5)]

into this:

 index  apple_red apple_green apple_yellow apple_pink
 1       3         2           9            50
 2       1         5           14           0

Note that tuples are unique per index in the original table Any idea how do to this ?

Thanks

1
  • 1
    SO is no code-writing service. Please post what you've tried so far. What problems did you encounter? Commented Jul 19, 2017 at 11:25

1 Answer 1

1

You can use:


df1 = pd.DataFrame([dict(x) for x in df['apple']], index=df.index) \
        .stack() \
        .astype(int) \
        .reset_index(name='val') \
        .drop_duplicates(['level_1','val']) \
        .set_index(['index', 'level_1'])['val'] \
        .unstack(fill_value=0) \
        .add_prefix('apple_') \
        .rename_axis(None) \
        .rename_axis(None, axis=1) \
        .reset_index()
print (df1)
   index  apple_green  apple_pink  apple_red  apple_yellow
0      1            2          50          3             9
1      2            5           0          1            14
Sign up to request clarification or add additional context in comments.

1 Comment

Glad can help, it was interesting problem!

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.