![](/img/trans.png)
[英]Pandas Dataframe - replace NaN with 0 if column value condition
[英]Pandas dataframe two column condition and replace
我有一個數據框,我想為兩列設置過濾器或條件,如果值未通過閾值更改為零,我想更改值,我知道我可以通過轉換為單獨的 dataframe 來完成過濾器和合並還有其他有效的方法嗎,請建議我。
import pandas as pd
df = pd.DataFrame({"User": ["user1", "user2", "user2", "user3", "user2", "user1"],
"Amount": [10.0, 1.0, 8.0, 2, 7.5, 8.0],
"Amount2": [1, 5.0, 8.0, 10.5, 0, 8.0]})
output 我想要 >2 閾值
User Amount Amount2
user1 10.0 0.0
user2 0.0 5.0
user2 8.0 8.0
user3 0.0 10.5
user2 7.5 0.0
user1 8.0 8.0
您可以使用numpy.where
一次處理所有所需的列:
# select desired columns (here based on name)
cols = df.filter(like='Amount').columns
# it's also possible to manually set them
# cols = ['Amount', 'Amount2']
df[cols] = np.where(df[cols].le(2), 0, df[cols]) # or .lt(2) for <
更新df
:
User Amount Amount2
0 user1 10.0 0.0
1 user2 0.0 5.0
2 user2 8.0 8.0
3 user3 0.0 10.5
4 user2 7.5 0.0
5 user1 8.0 8.0
您可以將低於2
的值clip
為2
,然后將2
替換為0
df[['Amount', 'Amount2']] = df[['Amount', 'Amount2']].clip(lower=2).replace(2, 0)
print(df)
User Amount Amount2
0 user1 10.0 0.0
1 user2 0.0 5.0
2 user2 8.0 8.0
3 user3 0.0 10.5
4 user2 7.5 0.0
5 user1 8.0 8.0
threshold = 2
df.loc[(df['Amount'] < threshold),'Amount'] = 0
df.loc[(df['Amount2'] < threshold),'Amount2'] = 0
您可以使用 np.where:
import numpy as np
df['Amount'] = np.where(df['Amount'] < 2,0, df['Amount'])
df['Amount2'] = np.where(df['Amount2'] < 2,0, df['Amount2'])
或者,如果您的 dataframe 中只有這些列:
df = df.where(df < 2, 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.