Transform Shape of a Pandas Dataframe -


i transform dataframe table below. similar reverse one-hot encoding, not same. there elegant way it?

from

enter image description here

to

enter image description here

df=pd.dataframe({'17-07-05': {3142: 1.0, 3214: np.nan}, '17-07-12': {3142: 1.0, 3214: np.nan}, '17-07-19': {3142: np.nan, 3214: np.nan}, 'website_id': {3142: 3142, 3214: 3214}}) 

pd.melt

pd.melt(df, 'website_id', var_name='date').sort_values('website_id')     website_id      date  value 0        3142  17-07-05    1.0 2        3142  17-07-12    1.0 4        3142  17-07-19    nan 1        3214  17-07-05    nan 3        3214  17-07-12    nan 5        3214  17-07-19    nan 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -