【发布时间】:2022-01-17 22:39:19
【问题描述】:
我想根据其他列的值创建一个 Pandas 列。以下是我的代码:
import pandas as pd
import numpy as np
raw_data = {'col1': [23,45,21],'col2': ['we', 'ee', 'cv'],'col3': ['y1', 'y2', 'y3']}
df = pd.DataFrame(raw_data, columns = ['col1','col2','col3'])
df['newCol'] = [
'N1' if c1 == 'we' else
'N2' if c1 == 'ee' | np.isin(c2, ['y2', 'y3']) else
'N3'
for c1, c2 in zip(df['col2'], df['col3'])
]
以上代码因错误而失败
TypeError: ufunc 'bitwise_or' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
您能帮我理解一下我的代码出了什么问题吗?
【问题讨论】:
-
搜索
np.select或np.where。
标签: python python-3.x pandas