程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Apply 和 Lambda 函数 - ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 修复更好大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Apply 和 Lambda 函数 - ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 修复更好?

开发过程中遇到Apply 和 Lambda 函数 - ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 修复更好的问题如何解决?下面主要结合日常开发的经验,给出你关于Apply 和 Lambda 函数 - ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 修复更好的解决方法建议,希望对你解决Apply 和 Lambda 函数 - ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 修复更好有所启发或帮助;

我正在尝试使用 Apply 和 Lambda 函数创建一个新列,如果“性别”为“女性”,则将“ID”值除以 219,如果“性别”为“男性”,则将其除以 393。首先,我尝试使用“性别”列,该列具有不起作用的分类变量。所以我创建了一个基于“性别”的二进制列,将 0 分配给“女性 1 到“男性”并使用该列,但没有用。

我仍然收到这样的值错误:

ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 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'],'Career','Personal',428634,'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 的使用效果很好

values = [
    ['Female','Access',96],['Female','Career',165],['Male',236],'Energy',445]
]
df: pd.DataFrame = pd.DataFrame(values,columns=['gender','Label','ID'])
df['newcol'] = df.apply(lambda row: row['ID'] / 219 if row['gender'] == 'Female' else row['ID'] / 393,axis=1)
print(df)

   gender   Label   ID    newcol
0  Female  Access   96  0.438356
1  Female  Career  165  0.753425
2    Male  Access  236  0.600509
3    Male  Energy  445  1.132316

更好

使用numpy.where

import numpy as np
import pandas as pd

values = [['Female',445]]
df: pd.DataFrame = pd.DataFrame(values,'ID'])
df['newcol'] = np.where(df['gender'] == 'Female',df['ID'] / 219,df['ID'] / 393)
print(df)

大佬总结

以上是大佬教程为你收集整理的Apply 和 Lambda 函数 - ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 修复更好全部内容,希望文章能够帮你解决Apply 和 Lambda 函数 - ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 修复更好所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。