I have a dataframe like:
df = pd.DataFrame({30: {'2020-10-09': 12.79, '2020-10-12': 12.83, '2020-10-13': 12.88, '2020-10-14': 12.93, '2020-10-15': 12.99, '2020-10-16': 13.07, '2020-10-19': 13.16, '2020-10-20': 13.24, '2020-10-21': 13.32, '2020-10-22': 13.42}, 365: {'2020-10-09': 12.27, '2020-10-12': 12.27, '2020-10-13': 12.28, '2020-10-14': 12.29, '2020-10-15': 12.29, '2020-10-16': 13.07, '2020-10-19': 12.31, '2020-10-20': 12.32, '2020-10-21': 12.32, '2020-10-22': 12.33}})
I want to find the rows where the values are equal across all columns. I can do this with .loc
but that would mean I have to hard code the column names, which I don't want to do as it is possible I will have more columns to compare in the future. I am quite close with
df.eq(df.iloc[:, 0], axis=0)
Which gives
30 365
2020-10-09 True False
2020-10-12 True False
2020-10-13 True False
2020-10-14 True False
2020-10-15 True False
2020-10-16 True True
2020-10-19 True False
2020-10-20 True False
2020-10-21 True False
2020-10-22 True False
but I am unable to retrieve the rows with True
in each column. I thought using df[df.eq(df.iloc[:, 0], axis=0)]
should work but it gives a multi-index error. Thanks!
You can add DataFrame.all
for test if all True
s and use boolean indexing
:
df = df[df.eq(df.iloc[:, 0], axis=0).all(axis=1)]
print (df)
30 365
2020-10-16 13.07 13.07
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.