2

It is intrducing nulls in resultant dataframe df.pivot( columns='colname',values='value')

Initial DF:

    colname    value
0   bathrooms   1.0
1   bathrooms   2.0
2   bathrooms   1.0
3   bathrooms   2.0
4   property_id 82671.0

enter image description here
Result:

colname   addr_street   bathrooms   bedrooms    lat lng parking_space   property_id
0           NaN            1.0        NaN       NaN NaN NaN              NaN
1           NaN            2.0        NaN       NaN NaN NaN              NaN
2           NaN            1.0        NaN       NaN NaN NaN              NaN

I just want a dataframe where unique values of 'colname' in initial df are the columns and the its corresponding value is the value(like it happens in bathroom)

cs95
  • 379,657
  • 97
  • 704
  • 746

1 Answers1

3

If I understand, you want a groupby and concatenation, not pivot:

df = pd.concat(
        {k: g.reset_index(drop=True) 
            for k, g in df.groupby('colname')['value']}, axis=1)

df
   bathrooms  property_id
0        1.0      82671.0
1        2.0          NaN
2        1.0          NaN
3        2.0          NaN
cs95
  • 379,657
  • 97
  • 704
  • 746