Skip to content
Advertisement

Tag: dataframe

Change dates to quarters in JSON file Python

I’m trying to convert the dates inside a JSON file to their respective quarter and year. My JSON file is formatted below: The current code I’m using is an attempt of using the pandas.Series.dt.quarter as seen below: The issue I face is that my code isn’t comprehending the object name “lastDate”. My ideal output should have the dates ultimately replaced

How to use pd.apply() to instantiate new columns?

Instead of doing this: I want to do this in one line or function. Below is what I tried: But I just get Exception has occurred: ValueError. What can I do here? Answer Looks like you can replace your whole code with a reindex: NB. By default the fill value is NaN, if you really want None use fill_value=None. If

Pandas lagged rolling average on aggregate data with multiple groups and missing dates

I’d like to calculate a lagged rolling average on a complicated time-series dataset. Consider the toy example as follows: This results in the following DataFrame: Now I’d like to add a column representing the average weight per fruit for the previous 7 days: wgt_per_frt_prev_7d. It should be defined as the sum of all the fruit weights divided by the sum

How to convert a 5-level dictionary into a DataFrame?

I have a dictionary with structure: Level 1: id (int) username (str) meta (contain a string of Kpi_info) This is a dictionary: My desire result is a DataFame like this: id username Year Month revenue kpi result 206 hantran 2021 1 2000 2100 0 206 hantran 2021 2 2500 2000 1 206 hantran 2022 1 3000 2500 1 206 hantran

Advertisement