【发布时间】:2021-12-02 22:35:45
【问题描述】:
我对此还不太了解,但我不知道如何使这项工作:
# Import pandas library
import pandas as pd
import numpy as np
# initialize list of lists
data = [[1, 0], [4, 0], [8, 0]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Value', 'Test'])
df['Test'] = np.where(df['Value'].astype(int) >= 3 & df['Value'].astype(int) <= 7, 1, 2)
# print dataframe.
print(df)
我收到错误:
ValueError:Series 的真值不明确。使用a.empty, a.bool()、a.item()、a.any() 或 a.all()。
对于此案的任何帮助将不胜感激。它应该做的是根据“值”列中的值的两个条件来更改“测试”列中的值
【问题讨论】:
-
使用
np.where((df['Value'] >= 3) & (df['Value'] <= 7), 1, 2)。 -
这正是我所缺少的——我很傻:(谢谢你的回答。
标签: pandas numpy conditional-statements