Search code examples
pythonpandasdcast

Creating a 'presence-absence' matrix from a pandas dataframe


I have data in the form of a pandas dataframe similar to that described here. That is:

Species     Site 
Panthera    A 
Panthera    B 
Panthera    C 
Neofelis    B 
Neofelis    D

And I want to create a presence-absence matrix like so:

Site    Panthera    Neofelis
A          1           0
B          1           1
C          1           0
D          0           1

How would I go about performing this operation in Python as opposed to R?


Solution

  • >>> pd.crosstab(df['Site'], df['Species'])
    Species  Neofelis  Panthera
    Site
    A               0         1
    B               1         1
    C               0         1
    D               1         0