python - How to convert Panda's columns into an index and a header when index column has duplicates -
i’d convert dataframe, df, similar one:
pidm | course | grade 1 | mat1 | b 1 | phy2 | c 2 | mat1 | 2 | mat2 | b 2 | phe2 |
to following format:
pidm | mat1 | phy2 | mat2 | phy 2 1 | b | c | nan | nan 2 | | nan | b |
i assuming like:
df2 = df.pivot(index='pidm', columns=‘course’, values = ‘grade)
but receive error stating have duplicate indices. thank help.
you can use pivot_table
aggregate function join
:
df2 = df.pivot_table(index='pidm', columns='course', values = 'grade', aggfunc=', '.join) print (df2) course mat1 mat2 phe2 phy2 pidm 1 b none none c 2 b none
Comments
Post a Comment