我尝试过使用许多答案中的代码来回答与此类似的问题,但是当我试图设置多个条件来决定一个列的值时,我没有找到任何对我有用的方法—我还想用3种不同的方式来实现这一点。在
我得到的数据如下:col1 col2 col3 col4 col5
1 1 1 4 1
0 1 1 1 1
0 0 1 1 1
我想添加另一列,取决于列1-5的值是否大于等于1
看起来像这样:
^{pr2}$
我试过这样的代码:df = pd.read_csv('file.csv',header=0)
m1 = df.col1 >= 1 & df.col2 >= 1 & df.col3 >= 1 & df.col4 >= 1 & df.col5 >= 1
m2 = df.col2 >= 1 & df.col3 >= 1 & df.col4 >= 1 & df.col5 >= 1
m3 = df.col3 >= 1 & df.col4 >= 1 & df.col5 >= 1
df['category'] = np.select([m1, m2, m3], ['certain', 'possible', 'probable'], default='Other')
但这在第一行给出了一个错误:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
为了理解这个错误,我是否需要在运行此代码之前设置值>;=1为True,其他值为False?在