How to find if a value unique to first data frame when comparing to another data frame
Question:
For example, I have 2 dataframes with 2 columns:
df1 df2
AAA BBB AAA KKK
BBB CCC BBB LLL
CCC FFF CCC FFF
DDD None None None
I want to spot whats on df1 is not in df2, then the result is DDD
(exclude None). How can I achieve this?
Answers:
import pandas as pd
df1 = pd.DataFrame([['AAA', 'BBB'], ['BBB', 'CCC'], ['CCC', 'FFF'], ['DDD', None]])
df2 = pd.DataFrame([['AAA', 'KKK'], ['BBB', 'LLL'], ['CCC', 'FFF'], [None, None]])
df1_uniq = []
df2_uniq = []
for col in df1.columns:
for string in df1[col].unique():
df1_uniq.append(string)
for col in df2.columns:
for string in df2[col].unique():
df2_uniq.append(string)
result = [x for x in df1_uniq if not x in df2_uniq]
print(result)
For example, I have 2 dataframes with 2 columns:
df1 df2
AAA BBB AAA KKK
BBB CCC BBB LLL
CCC FFF CCC FFF
DDD None None None
I want to spot whats on df1 is not in df2, then the result is DDD
(exclude None). How can I achieve this?
import pandas as pd
df1 = pd.DataFrame([['AAA', 'BBB'], ['BBB', 'CCC'], ['CCC', 'FFF'], ['DDD', None]])
df2 = pd.DataFrame([['AAA', 'KKK'], ['BBB', 'LLL'], ['CCC', 'FFF'], [None, None]])
df1_uniq = []
df2_uniq = []
for col in df1.columns:
for string in df1[col].unique():
df1_uniq.append(string)
for col in df2.columns:
for string in df2[col].unique():
df2_uniq.append(string)
result = [x for x in df1_uniq if not x in df2_uniq]
print(result)