将新列插入pandas pivot_table

时间:2018-02-04 19:49:50

标签: python-3.x pandas

格式化并分组我的DataFrame后,我创建了以下pivot_table,它显示了给定凭据的M和F的数量:

Gender            F         M
Credential                   
AA            105.0     117.0
AU            870.0     249.0
CNA          6309.0    5276.0

我接下来要做的是插入另一列,其中给定凭证的比率为F到M.例如:

Gender            F         M    Ratio
Credential                       
AA            105.0     117.0    0.8974
AU            870.0     249.0    3.4939
CNA          6309.0    5276.0    1.1957

我遇到了无法添加新列的问题,因为它不是一个类别。

TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category

我相信这是有道理的,因为没有"性别"叫"比率"那不是我想要的。有没有人有什么建议?我认为它会如此简单:

df["Ratio"] = df["F"] / df["M"]

但显然不是。

编辑:

如果没有为原始DataFrame输入新列,可能是从原始DataFrame中提取数据并使用该Ratio列创建新的DataFrame?

EDIT2:

以下是重现的步骤:

df = pd.DataFrame({
    'Credential': np.random.choice(['AA', 'AU', 'CNA'], 100), 
    'Gender': np.random.choice(['F', 'M'], 100), 
    'Counts': np.random.randn(100)
    })
df['Gender'] = df['Gender'].astype('category')
dfNew = pd.pivot_table(df, values='Counts', index='Credential', columns="Gender")
dfNew["Ratio"] = dfNew["F"] / dfNew["M"]

EDIT3(解决方案代码):

以下是解决错误的步骤:

df = pd.DataFrame({
    'Credential': np.random.choice(['AA', 'AU', 'CNA'], 100), 
    'Gender': np.random.choice(['F', 'M'], 100), 
    'Counts': np.random.randn(100)
    })
df['Gender'] = df['Gender'].astype('category')
dfNew = pd.pivot_table(df, values='Counts', index='Credential', columns="Gender")

# Changing from categoricals
dfNew.columns = dfNew.columns.tolist()

dfNew["Ratio"] = dfNew["F"] / dfNew["M"]

0 个答案:

没有答案
相关问题