pandas 如何创建没有聚合的简单交叉表?
pandas how to create simple cross-tab without aggregation?
我有一个包含 3 列的 pandas table:parent_male、parent_female、后代 - 所有字符串。
我想创建一个简单的稀疏交叉表 table 的男性与女性和后代作为值 - 我怎么能写一个 aggfunc 这样做。 (不需要真正的聚合)- 只需在空白处输入一个空字符串即可。
IIUC 你需要 pivot
:
df = df.pivot(index='parent_male', columns='parent_female', values='offsprings')
如果报错:
ValueError: Index contains duplicate entries, cannot reshape
所以最终的解决方案是:
ct = pd.pivot_table(d['male'], d['female'], d['offsprings'], aggfunc=','.join)
我在这里找到了答案...
Pandas Groupby Agg Function Does Not Reduce
我使用了这些信息。从上面的评论...
ct = pd.crosstab(d['male'], d['female'], d['offsprings'], aggfunc=','.join)
我有一个包含 3 列的 pandas table:parent_male、parent_female、后代 - 所有字符串。 我想创建一个简单的稀疏交叉表 table 的男性与女性和后代作为值 - 我怎么能写一个 aggfunc 这样做。 (不需要真正的聚合)- 只需在空白处输入一个空字符串即可。
IIUC 你需要 pivot
:
df = df.pivot(index='parent_male', columns='parent_female', values='offsprings')
如果报错:
ValueError: Index contains duplicate entries, cannot reshape
所以最终的解决方案是:
ct = pd.pivot_table(d['male'], d['female'], d['offsprings'], aggfunc=','.join)
我在这里找到了答案... Pandas Groupby Agg Function Does Not Reduce 我使用了这些信息。从上面的评论...
ct = pd.crosstab(d['male'], d['female'], d['offsprings'], aggfunc=','.join)