Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas how to create simple cross-tab without aggregation?

I have a pandas table with 3 columns: parent_male, parent_female, offsprings - all strings. I want to create a simple sparse crosstab table of male vs female and the offsprings as the values - how can I write an aggfunc that do so. (no real aggregation is needed) - just put an empty string in the blanks.

like image 508
Dror Hilman Avatar asked Jan 06 '23 03:01

Dror Hilman


2 Answers

IIUC you need pivot:

df = df.pivot(index='parent_male', columns='parent_female', values='offsprings')

If get error:

ValueError: Index contains duplicate entries, cannot reshape

use pivot_table

So final solution is:

ct = pd.pivot_table(d['male'], d['female'], d['offsprings'], aggfunc=','.join)
like image 59
jezrael Avatar answered Jan 10 '23 02:01

jezrael


I found the answer here... Pandas Groupby Agg Function Does Not Reduce and I used the info. from the comments above...

ct = pd.crosstab(d['male'], d['female'], d['offsprings'], aggfunc=','.join)
like image 26
Dror Hilman Avatar answered Jan 10 '23 02:01

Dror Hilman