I have a df that looks like this;
col1 col2
aa aa
cc bb
dd dd
How do I compare both columns in the same df but drop the duplicates and keep the unique in their respective columns?
new df:
col1 col2
cc bb
We can construct a filter that checks if the value of df.col1
is different from df.col2
, and then filter, like:
df[df.col1 != df.col2]
For example:
>>> df = pd.DataFrame([['aa', 'aa'], ['cc', 'bb'], ['dd', 'dd']], columns=['col1', 'col2'])
>>> df
col1 col2
0 aa aa
1 cc bb
2 dd dd
>>> df[df.col1 != df.col2]
col1 col2
1 cc bb
We here construct a new dataframe, but we can thus set df
to the new dataframe, like:
df = df[df.col1 != df.col2]
这应该可以解决问题:
df[df[col1] != df[col2]]
If you just want to extract the rows that have the same value across columns, this should do.
import pandas as pd
data = {'a':[40, 30, 10],
'b':[40, 20, 10]}
df = pd.DataFrame(data)
df = df[~(df['a']==df['b'])]
Output
>>> df
a b
0 10 40
2 30 10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.