![](/img/trans.png)
[英]Python groupby - change column values based on conditions in other columns
[英]How to set values of a column based on multiple conditions in other columns in python?
我嘗試使用許多答案中的代碼來解決與此問題類似的問題,但是當我嘗試設置確定列值的多個條件時,我沒有發現任何對我有用的東西-我也想在3中做到這一點不同的方式。
我的數據如下所示:
col1 col2 col3 col4 col5
1 1 1 4 1
0 1 1 1 1
0 0 1 1 1
我想添加另一列取決於列1-5是否具有> = 1的值,如下所示:
col1 col2 col3 col4 col5 category
1 1 1 4 1 certain
0 1 1 1 1 probable
0 0 1 1 1 possible
我試過這樣的代碼:
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?
定義條件時缺少括號。 其背后的原因是按位運算符的優先級高於比較器。 而是使用:
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')
結果為預期的輸出:
col1 col2 col3 col4 col5 category
0 1 1 1 4 1 certain
1 0 1 1 1 1 possible
2 0 0 1 1 1 probable
這有效
df['cateogry'] = df.apply(lambda x: 'Certain' if sum(x.values >= 1) >= 5 else 'Probable' if sum(x.values >= 1) >= 4 else 'Possible' , axis=1)
產量
col1 col2 col3 col4 col5 cateogry
0 1 1 1 4 1 Certain
1 0 1 1 1 1 Probable
2 0 0 1 1 1 Possible
創建一個函數並將其應用於數據框。
def create_new_column(row):
if row['column1'] > 1 and row['column2'] > 1:
return 1
else:
return 0
df['new_column'] = df.apply(lambda x: create_new_column(x), axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.