Reputation: 1568
I have a pandas df:
df
DEtoDK DKtoDE
self other self other
2021-01-01 00:00:00+00:00 0.0 NaN 2230.08 NaN
2021-01-01 01:00:00+00:00 0.0 0.0 1887.72 2230.08
2021-01-01 02:00:00+00:00 0.0 0.0 1821.33 1887.72
2021-01-01 03:00:00+00:00 0.0 0.0 1743.20 1821.33
2021-01-01 04:00:00+00:00 0.0 0.0 1720.78 1743.20
... ... ... ... ...
2021-05-31 19:00:00+00:00 0.0 0.0 782.88 892.16
2021-05-31 20:00:00+00:00 0.0 0.0 872.96 782.88
2021-05-31 21:00:00+00:00 0.0 0.0 1165.36 872.96
2021-05-31 22:00:00+00:00 0.0 0.0 1418.32 1165.36
2021-05-31 23:00:00+00:00 0.0 0.0 1393.28 1418.32
[3624 rows x 4 columns]
I would like to filter this df, with some conditions like, if the (DEtoDK, self)
or (DKtoDE, self)
values are 0. For that I am using the following:
df.loc[(df[('DEtoDK', 'self')].values == 0) | (df[('DKtoDE', 'self')].values == 0)]
And this works, however when the df does not have any 0 values then I was expecting to generate an empty dataframe, however I am getting a KeyError.
df.loc[(df[('DEtoDK', 'self')].values == 'test') | (df[('DKtoDE', 'self')].values == 'test')]
KeyError: False
so the conditions are generating False
, instead of an empty array, therefore pandas cannot locate. How can I fix this behaviour?
Upvotes: 2
Views: 88
Reputation: 71560
No need for values
:
df.loc[(df[('DEtoDK', 'self')] == 0) | (df[('DKtoDE', 'self')] == 0)]
Upvotes: 3