New column that counts the frequency that a value occurs in a shifted Pandas dataframe column

I have a dataframe that looks like ID Date feature 1 2020-05-01 2 1 2020-05-01 3 1 2020-05-01 4 2 2019-03-15 3 2 2019-03-15 2 3 2022-04-22 5 3 2022-04-22 8 3 2022-04-22 4 3 2022-04-22 2 4 2015-01-18 4 4 2015-01-18 6 4 2015-01-18 7 I sort it by time in descending order using… Read More New column that counts the frequency that a value occurs in a shifted Pandas dataframe column

Apply calculation for dataframe columns for multiple dataframes at the same time

I am creating multiple dataframes for each unique value in a column. It works properly. regions = dataDF[‘region’].unique().tolist() df_dict = {name: dataDF.loc[dataDF[‘region’] == name] for name in regions} However, now I would like to calculate the average for the temperature and then calculate the mean afterward for every newly created dataframe. for df in df_dict:… Read More Apply calculation for dataframe columns for multiple dataframes at the same time

How to calculate cumulative sum based on months in a pandas dataframe?

I want to calculate cumulative sum of values in a pandas dataframe column based on months. code: import pandas as pd import numpy as np data = {‘month’: [‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’, ‘January’, ‘February’, ‘March’], ‘kpi’: [‘sales’, ‘sales quantity’, ‘sales’, ‘sales’, ‘sales’, ‘sales’, ‘sales’, ‘sales quantity’, ‘sales’, ‘sales’, ‘sales’, ‘sales’],… Read More How to calculate cumulative sum based on months in a pandas dataframe?

How to create column with the numbers 0 or 1 when substraction two columns in the same dataframe is less than zero?

In a simple example, I have a dataframe that looks like this (I am going to put in a dict structure, but it is really a dataframe): data = {‘value’: [1,2,3,4,5,1,3,4,6], ‘limit’: [4,4,4,4,3,3,3,1,1], } data = pd.Dataframe(data) I need add an extra column to dataframe that look like this: data = {‘value’: [1, 2, 3,… Read More How to create column with the numbers 0 or 1 when substraction two columns in the same dataframe is less than zero?