I have a pandas Dataframe named df and it has a column named logvalues. I want to create a new column, violatedInstances based on these log values.
If Max >= logvalue >= Min assign 0 (Not violated) If logvalue > Max or logvalue < Min assign 1 (Violated)
#create DataFrame
df_x = pd.DataFrame({'logvalue': ['20', '20.5', '18.5', '2', '10'],
'ID': ['1', '2', '3', '4', '5']})
Max = 20
min = 15
Output should look like below.
| logvalue | ID | violatedInstances |
| ------------ | ------------ | ------------ |
| 20 | 1 | 0 |
| 20.5 | 2 | 1 |
| 18.5 | 3 | 0 |
| 2 | 4 | 1 |
| 10 | 5 | 1 |
Sorry for asking this simple question. I tried several methods but nothing worked. How can I do this in pandas?
3条答案
按热度按时间h22fl7wq1#
首先,我会将logvalue转换为float,以便您可以执行比较
df_x['logvalue'] = df_x['logvalue'].astype('float')
那么你可以这样使用numpy:
其输出:
xxhby3vn2#
您的
logvalue
类型是string
,因此您必须转换为float:wj8zmpe13#
实验结果: