我试图通过使用Apply和Lambda函数创建一个新列,将'ID'值除以219(如果'Gender'是'femal',如果'Gender'是'Male',则除以393)。首先,我尝试使用“性别”列,该列包含无法使用的分类变量。因此,我创建了一个基于“性别”的二进制列,将0分配给“女性1”分配给“男性”,并使用该列,但不起作用
我仍然得到如下值错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我的代码是
by_label_out_degree[NormID] = by_label_out_degree.apply(lambda row: row['ID']/ 219 if row['Gender2'] == 0 else row['ID']/ 393,axis=1)
我再次添加我的代码!谢谢你的帮助
values = [
[42785,428855,'Energy','Female'],
[43432,428686,'Trust','Male'],
[43432,428686,'Career','Male'],
[43432,428686,'Personal','Male'],
[43432,428634,'Trust','Female']
]
df: pd.DataFrame = pd.DataFrame(values, columns =['ID','Target','Label','Gender'])
new_df = df.groupby(['Gender','Label']).ID.count().reset_index()
new_df['Gender2'] = new_df.Gender.map({'Female':0,'Male':1})
new_df['NormID'] = new_df.apply(lambda row: row['ID']/219 if row['Gender2'] == 0 else row['ID']/393, axis = 1)
修复
使用列
Gender
效果很好更好
使用^{}
相关问题 更多 >
编程相关推荐