I currently have a dataframe that looks like this
DATE ID CHANGE
05-05-2020 333333 10
05-05-2020 333334 2
05-05-2020 333335 8
05-06-2020 333333 -2
05-06-2020 333334 4
05-06-2020 333335 5
and I want to be able to convert that dataframe horizontally by the date and separated by the ID to either
DATE ID CHANGE ID CHANGE ID CHANGE
05-05-2020 333333 10 333334 2 333335 8
05-06-2020 333333 -2 333334 4 333335 5
or
DATE 333333_CHANGE 333334_CHANGE 333335_CHANGE
05-05-2020 10 2 8
05-06-2020 -2 4 5
I assume you have to do some kind of df.groupby()
or pd.concat()
though reading the documentations for them makes me confused as well.
If possible, output#2 is better, because duplicate column names can lead to many different issues in output#1.
For output #2, Use .unstack()
and then do some cleanup on the multi-index columns for a better format:
df = df.set_index(['DATE', 'ID']).unstack(1).add_suffix('_CHANGE')
df.columns = df.columns.droplevel()
df = df.reset_index()
df
Out[1]:
ID DATE 333333_CHANGE 333334_CHANGE 333335_CHANGE
0 05-05-2020 10 2 8
1 05-06-2020 -2 4 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.