Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13921

How to check if there's a specific number of records for a unique_id in a df?

$
0
0

I have a df like this:

data = {'ds': ['2024-02-01', '2024-01-01', '2023-12-01', '2024-02-01', '2023-12-01'],'y': [500, 600, 700, 800,500],'unique_id': [1, 1, 1, 2, 2]}input_df = pd.DataFrame(data)

I need a function to select the unique ids where the data is valid according to a minimum consecutive observations' parameter from last month backwards.

The expected output for a min_consecutive_observations == 2 is:

expected_output = {'ds': ['2024-02-01', '2024-01-01', '2023-12-01'],'y': [500, 600, 700],'unque_id': [1, 1, 1]}

I'm having trouble coding a function that achieves this.

I got last month like this:

last_month_input = datetime.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) - pd.DateOffset(months=1)

But can't wrap my ahead around on how to check for continuity from the last_month_input backwards.


Viewing all articles
Browse latest Browse all 13921

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>