我有一個這樣的資料框:
col0 col1 col2 col3 col4
0 0 0 0 1 0
1 0 0 1 0 0
2 0 0 0 0 0
3 0 0 0 0 0
如果該條目沒有更早出現,我如何創建一個具有相同條目但最后一列為 1 的新條目?它應該如下所示:
col0 col1 col2 col3 col4
0 0 0 0 1 0
1 0 0 1 0 0
2 0 0 0 0 1
3 0 0 0 0 1
uj5u.com熱心網友回復:
讓我們嘗試將前面的列與 1 進行比較,并檢查每行中是否存在 1
condition.iloc[condition.iloc[:, :-1].eq(1).sum(axis=1).eq(0), -1] = 1
# or
condition.iloc[~condition.iloc[:, :-1].eq(1).any(axis=1), -1] = 1
print(condition)
0 1 2 3 4
0 0 0 0 1 0
1 0 0 1 0 0
2 0 0 0 0 1
3 0 0 0 0 1
uj5u.com熱心網友回復:
沒有索引怎么辦:
condition.drop(condition.columns[len(condition.columns)-1],axis=1,inplace=True)
print(pd.concat([condition,(~condition.any(1)).astype(int)],axis=1).T.reset_index(drop=True).T)
0 1 2 3 4
0 0 0 0 1 0
1 0 0 1 0 0
2 0 0 0 0 1
3 0 0 0 0 1
uj5u.com熱心網友回復:
代碼
# Make a New dataframe with the same value
condition_2 = condition.copy()
# For each row, change last column to 1 if no 1 in previous columns
condition_2.iloc[:, -1] = np.where(~(condition.iloc[:, :-1]==1).any(axis = 1), 1, condition_2.iloc[:, -1])
解釋
condition_2.iloc[:, -1] # last column of dataframe condition_2
~(condition.iloc[:, :-1]==1).any(axis = 1) mask which is True if not a 1 in previous column of each row
np.where(...) # allows for obtaining indexes where mask condition is satisfied
uj5u.com熱心網友回復:
您可以使用以下方法any:
df['col4'] = (~df.any(axis=1)).astype(int)
如果您需要忽略最后一列:
df['col4'] = (~df.drop('col4', axis=1).any(axis=1)).astype(int)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517074.html
上一篇:洗掉和排序大資料框中的列
