[英]create column in dataframe pandas, max between column and value
我嘗試在數據框熊貓中創建列,在列和值之間創建最大值
const = 10
df['new_val'] = max(df['val']-const, 0)
我收到此錯誤:
ValueError:Series 的真值不明確。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
輸入:
val |
12 |
5 |
預期的:
val | new_val
12 | 2
5 | 0
使用numpy.maximum
作為每列val
的max
:
const = 10
df['new_val'] = np.maximum(df['val'].sub(const), 0)
print (df)
val new_val
0 12 2
1 5 0
或者用Series.clip
減去負值的0
值:
const = 10
df['new_val'] = df['val'].sub(const).clip(lower=0)
print (df)
val new_val
0 12 2
1 5 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.