Pandas: Resample dataframe column, get discrete feature that corresponds to max value

2024/11/10 14:01:08

Sample data:

import pandas as pd
import numpy as np
import datetimedata = {'value': [1,2,4,3], 'names': ['joe', 'bob', 'joe', 'bob']}
start, end = datetime.datetime(2015, 1, 1), datetime.datetime(2015, 1, 4)
test = pd.DataFrame(data=data, index=pd.DatetimeIndex(start=start, end=end, freq="D"), columns=["value", "names"])

gives:

          value names
2015-01-01  1   joe
2015-01-02  2   bob
2015-01-03  4   joe
2015-01-04  3   bob

I want to resample by '2D' and get the max value, something like:

df.resample('2D')

The expected result should be:

          value names2015-01-01 2   bob2015-01-03 4   joe

Can anyone help me?

Answer

You can resample to get the arg max of value and then use it to extract names and value

(df.resample('2D')[['value']].idxmax().assign(names=lambda x: df.loc[x.value]['names'].values,value=lambda x: df.loc[x.value]['value'].values)
)
Out[116]: value names
2015-01-01      2   bob
2015-01-03      4   joe
https://en.xdnf.cn/q/72352.html

Related Q&A

How to filter string in multiple conditions python pandas

I have following dataframeimport pandas as pd data=[5Star,FiveStar,five star,fiv estar] data = pd.DataFrame(data,columns=["columnName"])When I try to filter with one condition it works fine.d…

Is there a way to use a dataclass, with fields with defaults, with __slots__

I would like to put __slots__ on a dataclass with fields with defaults. When I try do that, I get this error: >>> @dataclass ... class C: ... __slots__ = (x, y, ) ... x: int ... y:…

Read remote file using python subprocess and ssh?

How can I read data from a big remote file using subprocess and ssh?

Django - get_queryset() missing 1 required positional argument: request

I was trying to make an API using REST Framework for uploading a file to the server and my codes are below.If you have any other easy method to do the same please post your code.models.pyfrom django.db…

Storing elements of one list, in another list - by reference - in Python?

I just thought Id jot this down now that Ive seen it - it would be nice to get a confirmation on this behavior; I did see How do I pass a variable by reference?, but Im not sure how to interpret it in…

Joining Two Different Dataframes on Timestamp

Say I have two dataframes:df1: df2: +-------------------+----+ +-------------------+-----+ | Timestamp |data| | Timestamp |stuff| +-------------------+---…

Find if the array contain a 2 next to a 2

I am stuck on this problemGiven an array of ints, return True if the array contains a 2 next to a 2 somewhere.has22([1, 2, 2]) → True has22([1, 2, 1, 2]) → False has22([2, 1, 2]) → FalseI know the b…

AttributeError: xml.etree.ElementTree.Element object has no attribute encode

Im trying to make a desktop notifier, and for that Im scraping news from a site. When I run the program, I get the following error.news[child.tag] = child.encode(utf8) AttributeError: xml.etree.Element…

How to parse code (in Python)?

I need to parse some special data structures. They are in some somewhat-like-C format that looks roughly like this:Group("GroupName") {/* C-Style comment */Group("AnotherGroupName")…

Using OpenCV detectMultiScale to find my face

Im pretty sure I have the general theme correct, but Im not finding any faces. My code reads from c=cv2.VideoCapture(0), i.e. the computers videocamera. I then have the following set up to yield where …