[英]pandas dataframe update column values based on other columns of the corresponding row
[英]How to update pandas column cell values based on similarity of some elements with other column names on row level
我有一個看起來像這樣的數據框:
id tag1 tag2 tag3 col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
id1 col3 col4 col7 0 0 0 0 0 0 0 0 0 0
id2 col1 col2 col9 0 0 0 0 0 0 0 0 0 0
id3 col2 col5 col6 0 0 0 0 0 0 0 0 0 0
id4 col3 col6 col10 0 0 0 0 0 0 0 0 0 0
id5 col1 col7 col8 0 0 0 0 0 0 0 0 0 0
id6 col4 col6 col9 0 0 0 0 0 0 0 0 0 0
id7 col5 col7 col10 0 0 0 0 0 0 0 0 0 0
id8 col2 col3 col6 0 0 0 0 0 0 0 0 0 0
id9 col5 col9 col10 0 0 0 0 0 0 0 0 0 0
id10 col4 col8 col9 0 0 0 0 0 0 0 0 0 0
我需要得到一個“基本”數據框,顯示 1 列(col1 到 col10)值出現在標簽中的位置:
id tag1 tag2 tag3 col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
id1 col3 col4 col7 0 0 1 1 0 0 1 0 0 0
id2 col1 col2 col9 1 1 0 0 0 0 0 0 1 0
id3 col2 col5 col6 0 1 0 0 1 1 0 0 0 0
id4 col3 col6 col10 0 0 1 0 0 1 0 0 0 1
id5 col1 col7 col8 1 0 0 0 0 0 1 1 0 0
id6 col4 col6 col9 0 0 0 1 0 1 0 0 1 0
id7 col5 col7 col10 0 0 0 0 1 0 1 0 0 1
id8 col2 col3 col6 0 1 1 0 0 1 0 0 0 0
id9 col5 col9 col10 0 0 0 0 1 0 0 0 1 1
id10 col4 col8 col9 0 0 0 1 0 0 0 1 1 0
我真的不想使用三重循環,如:
cols = [el for el in df if el.startswith('col')]
tags = [el for el in df if el.startswith('tag')]
for index, row in df.iterrows():
for col in cols:
for tag in tags:
if row[tag] == col:
row[col] +=1
但即使這樣也行不通。 什么是最好的方法,上面有什么問題?
非常感謝!
的組合get_dummies
和updat
會給你想你想:
df.update(pd.get_dummies(df.filter(like='tag')
.stack()
).sum(level=0)
)
輸出:
id tag1 tag2 tag3 col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
-- ---- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ -------
0 id1 col3 col4 col7 0 0 1 1 0 0 1 0 0 0
1 id2 col1 col2 col9 1 1 0 0 0 0 0 0 1 0
2 id3 col2 col5 col6 0 1 0 0 1 1 0 0 0 0
3 id4 col3 col6 col10 0 0 1 0 0 1 0 0 0 1
4 id5 col1 col7 col8 1 0 0 0 0 0 1 1 0 0
5 id6 col4 col6 col9 0 0 0 1 0 1 0 0 1 0
6 id7 col5 col7 col10 0 0 0 0 1 0 1 0 0 1
7 id8 col2 col3 col6 0 1 1 0 0 1 0 0 0 0
8 id9 col5 col9 col10 0 0 0 0 1 0 0 0 1 1
9 id10 col4 col8 col9 0 0 0 1 0 0 0 1 1 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.