在資料中,幀匹配雙列,如果第二列中的任何值在第一列中可用,則從第二列中洗掉值
col1 col2
1
2 1
3 9
4
5 1
6 2
輸出
col1 col2
1
2
3 9
4
5
6
在這里,col2 中的 1 和 2 在 col1 中可用。所以,這個重復的資料應該被洗掉
uj5u.com熱心網友回復:
使用s.mask值匹配和替換,我們可以做一些類似的事情:
df['col2'] = df['col2'].mask(pd.to_numeric(df['col2']).isin(df['col1']), "")
col1 col2
0 1
1 2
2 3 9.0
3 4
4 5
5 6
uj5u.com熱心網友回復:
import pandas as pd
col1= [1,2,3,4,5,6]
col2= [0,0,9.0,0,0,0]
df = pd.DataFrame({'col1':col1, 'col2':col2})
# add column with no of occurrence of Non None values in the column name starts with 'a'
# iterate over columns
for col in df.columns:
# remove values that are in previous columns
for prev_col in df.columns[:df.columns.get_loc(col)]:
df[col] = df[col].where(~df[col].isin(df[prev_col]), None)
# OUTPUT
# col1 col2
# 0 1 0.0
# 1 2 0.0
# 2 3 9.0
# 3 4 0.0
# 4 5 0.0
# 5 6 0.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/504396.html
上一篇:python中的特殊陣列
