作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据框
:dt
以及列名称的列表
:nn_language
编辑:添加示例数据
dt = pd.DataFrame({"language1": ["english", "english123", "ingles", "ingles123", "14.0", "13", "french"],
"language2": ["englesh", "english123", "ingles", "ingles123", "14", "13", "french"]})
nn_language = dt.columns[dt.columns.str.contains("language")]
dt[nn_language]
的所有元素都是 object
类型。我想做的是将 dt[nn_language] 的初始值更改为 "english"
如果初始值是 like
("english","ingles",14)
否则我想将初始值更改为 "other"
我尝试过:dt[nn_language].apply(lambda x: 'english' if x.str.contains('^engl|^ingl|14.0') else 'other')
但我收到错误 ValueError: ('系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all ().',
最佳答案
使用isin
:
check = ["english","ingles", '14']
dt[nn_language].apply(lambda x: np.where(x.isin(check) , 'english', 'other'))
或者:
dt[nn_language].apply(lambda x: pd.Series(np.where(x.isin(check) , 'english', 'other')))
看来你需要:
dt[nn_language].apply(lambda x: np.where(x.str.contains('^engl|^ingl|14.0') , 'english', 'other'))
关于python - 在数据帧中使用带有 lambda 表达式的条件时出现 "ValueError: The truth value of a Series is ambiguous",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46218134/
我是一名优秀的程序员,十分优秀!