Pandas pivot table from CSV with checkmarks

Viewed 147

I have a CSV file which looks something like this

data_source destination description
A 1 A description
B 2 B description
C 3 C description
A 3 A description

and I'm looking to generate a pivot table that looks something like this

description data_source 1 2 3
A description A x x
B description B x
C description C x

The closest I've gotten so far is this

table = pd.pivot_table(df, index='data_source', columns='destination', values=['data_source'], aggfunc='first')

I tried using ['description', 'data_source'] as the index to get the description and data_source rows but I end up with a 0 column table. I also don't know the data type for what gets passed to aggfunc to output an 'x' in the valued cells.

I'm new to Pandas and I don't have a good mental model yet so any help is appreciated.

1 Answers

You are looking forpd.crosstab

out = pd.crosstab([df['data_source'],df['description']], df['destination']).reset_index()
Out[101]: 
destination data_source   description  1  2  3
0                     A  Adescription  1  0  1
1                     B  Bdescription  0  1  0
2                     C  Cdescription  0  0  1
Related