Reputation: 7457
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.
Upvotes: 2
Views: 8868
Reputation: 862731
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)
Upvotes: 6
Reputation: 7457
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)
Upvotes: 2