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.

1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.