0

I have a dataframe such as :

Groups Species Value 
G1     SP1     YES
G1     SP2     YES
G1     SP3     NO
G1     SP4     YES
G2     SP1     NO
G2     SP2     NO
G2     SP4     YES
G3     SP1     YES
G3     SP2     YES
G4     SP1     NO

And I would liek simply to pivot the table such as :

Species  G1  G2  G3  G4
SP1      YES NO  YES NO
SP2      YES NO  YES NA
SP3      NO  NA  NA  NA
SP4      YES YES NA  NA

So far I tried :

df.pivot(columns='Groups',index='Species',values=Value)

But I get :

ValueError: Index contains duplicate entries, cannot reshape
chippycentra
  • 3,396
  • 1
  • 6
  • 24
  • 1
    Use `df.pivot_table(columns='Groups',index='Species',values=Value, aggfunc=','.join)` – jezrael Jan 03 '23 at 14:29
  • 1
    `pivot` works just fine with sample data. The error is because there are duplicates in `Groups, Species`. Either use an aggfunc as jezrael commented or refer to Q/A 10 in the dup link. – Quang Hoang Jan 03 '23 at 14:43

0 Answers0