2

given the following data:

pd.DataFrame(dict(
    name = ['a', 'a', 'a', 'b', 'b', 'b'],
    vals = [1, 2 , 3, 99, 3, 4]
))

which looks as:

  name  vals
0    a     1
1    a     2
2    a     3
3    b    99
4    b     3
5    b     4

I'm wondering how to create the following:

     1     2    3      4     99
a  true  true  true  false  false
b  false false true  false  true

Note - having the exact values of true and false in the above aren't so important, I don't know how to go about creating a table of this type at the moment.

ALollz
  • 57,915
  • 7
  • 66
  • 89
baxx
  • 3,956
  • 6
  • 37
  • 75
  • 2
    The function exists in pandas: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.crosstab.html – r.ook May 01 '20 at 15:15

2 Answers2

6

Try this crosstab

s=pd.crosstab(df.name,df.vals).astype(bool)
Out[38]: 
vals     1      2     3      4      99
name                                  
a      True   True  True  False  False
b     False  False  True   True   True
halfer
  • 19,824
  • 17
  • 99
  • 186
BENY
  • 317,841
  • 20
  • 164
  • 234
2

Could also get_dummies and then aggregate along the names

pd.get_dummies(df.set_index('name').vals).any(level=0) 
                                        #.max(level=0) for 1/0 dummies
                                        #.sum(level=0) for counts
ALollz
  • 57,915
  • 7
  • 66
  • 89