Mass17
Mass17

Reputation: 1605

pandas: create a new dataframe from existing columns values

I have a dataframe like this;

ID          code   num
333_c_132   x       0
333_c_132   n36     1
998_c_134   x       0
998_c_134   n36     0
997_c_135   x       1
997_c_135   n36     0

From this I have to create a new dataframe like below; you can see a new column numX is formed with unique ID. Please note that numX values are taken from num column corresponding to n36.

ID          code   num   numX
333_c_132   x       0     1
998_c_134   x       0     0
997_c_135   x       1     0

How can I do this only using pandas?

Upvotes: 2

Views: 48

Answers (1)

anky
anky

Reputation: 75150

You can use a mask then merge after pivotting:

m = df['code'].eq('n36')
(df[~m].merge(df[m].set_index(['ID','code'])['num'].unstack()
                              ,left_on='ID',right_index=True))

          ID code  num  n36
0  333_c_132    x    0    1
2  998_c_134    x    0    0
4  997_c_135    x    1    0

Upvotes: 4

Related Questions