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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With