简体   繁体   中英

Select most recent example of multiindex dataframe

I have a similiar problem as in Getting the last element of a level in a multiindex . In the mentioned question the multiindex dataframe has for each group a start number which is always the same.

However, my problem is slightly different. I have again two columns. One column with an integer (in the MWE below it is a bool) and a second column with a datetime index. Similar, to the above example, I want select for each unique value in the first column the last row. In my example, it means the value with the most recent timestamp. The solution from the question above does not work, since I have no fixed start value for the second column.

MWE:

import pandas as pd

df = pd.DataFrame(range(10), index=pd.date_range(pd.Timestamp("2020.01.01"), pd.Timestamp("2020.01.01") + pd.Timedelta(hours=50), 10))
mask = (df.index.hour > 1) & (df.index.hour < 9)
df.groupby(mask)
df = df.groupby(mask).rolling("4h").mean()

The resulting dataframe looks like:

                             0
False 2020-01-01 00:00:00  0.0
      2020-01-01 11:06:40  2.0
      2020-01-01 16:40:00  3.0
      2020-01-01 22:13:20  4.0
      2020-01-02 09:20:00  6.0
      2020-01-02 14:53:20  7.0
      2020-01-02 20:26:40  8.0
True  2020-01-01 05:33:20  1.0
      2020-01-02 03:46:40  5.0
      2020-01-03 02:00:00  9.0

Now, I want to get for each value in the first column the row with the most recent time stamp. Ie, I would like to get the following dataframe:

                             0
False 2020-01-02 20:26:40  8.0
True  2020-01-03 02:00:00  9.0

I would really appreciate ideas like in the mentioned link which do this.

Assuming values in level 1 are sorted try with groupby tail :

out = df.groupby(level=0).tail(1)

out :

                             0
False 2020-01-02 20:26:40  8.0
True  2020-01-03 02:00:00  9.0

If not sort_index first:

out = df.sort_index(level=1).groupby(level=0).tail(1)

out :

                             0
False 2020-01-02 20:26:40  8.0
True  2020-01-03 02:00:00  9.0

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM