I'm comparing 2 dataframes by their row names and I want to get the name of the rows that don't match example:
check = sum(df1.index!=df2.index)
if check:
raise TypeError("Not Match")
else:
print("All OK")
When the index don't match I want to print the first instance of the row that didn't match. I've tried using a print statement as shown below but it doesn't execute.
check = sum(df1.index!=df2.index)
if check:
print(df1.index)
raise TypeError("Not Match")
else:
print("All OK")
The statement -
df1.index != df2.index
Returns a numpy array of bools
. Let's retain this for now. It may need a slight change to work -
m = df1.index.values != df2.index.values
To get the counts of True
(non-matches) in m
, call sum
-
if m.sum():
raise TypeError(...)
To get the first instance of no-match, index with m
:
print(df1[m].iloc[0])
Thanks to MaxU, here's a nicer solution using argmax
-
print(df1[m.argmax()])
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.