Dror Hilman Dror Hilman - 1 month ago 7
Python Question

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.

Answer

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

More information in in Pivoting with aggregating.

So final solution is:

ct = pd.pivot_table(d['male'], d['female'], d['offsprings'], aggfunc=','.join)
Comments