rank

How to give rank on datetime column group by another column with userid in it

How to give rank on datetime column group by another column with userid in it Question: My columns are ID, User_created_date and Customer city while user_created_date is in 2023-01-01 00:01:05+05:30 format now I wanted to know when did the first user was created based on each city and give rank to them based on city …

Total answers: 1

Adding rank column for every numerical column in pandas

Adding rank column for every numerical column in pandas Question: Here is an example of my dataframe (my actual dataframe has 20+ columns and 100+ rows) df = pd.DataFrame([[‘Jim’, 93, 87, 66], [‘Bob’, 88, 90, 65], [‘Joe’, 72, 100, 70]], columns=[‘Name’, ‘Score1’, ‘Score2’, ‘Score3’]) Name Score1 Score2 Score3 Jim 93 87 66 Bob 88 90 …

Total answers: 2

Pandas rank values within groupby, starting a new rank if diff is greater than 1

Pandas rank values within groupby, starting a new rank if diff is greater than 1 Question: I have a sample dataframe as follows: data={‘Store’:[1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2], ‘Week’:[1,2,3,4,5,6,19,20,21,22,1,2,50,51,52,60,61,62,70,71,72,73]} df=pd.DataFrame.from_dict(data) df[‘WeekDiff’] = df.groupby(‘Store’)[‘Week’].diff().fillna(1) I added a difference column to find the gaps in the Week column within my data. I have been trying to groupby Store and somehow use …

Total answers: 1

Pandas Rank for Text Column not by alphabetical order

Pandas Rank for Text Column not by alphabetical order Question: I’ve been trying to sort the following based on the order it currently is in within the Dataframe. I even created an ‘Indexing’ column to try to preserve the order, however when I do a rank for the ‘Level 2’ column it defaults to ordering …

Total answers: 2

Creating a ranking in Python from a given value by id

Creating a ranking in Python from a given value by id Question: I have this dataset: dic = {‘id’:[1,1,1,1,1,2,2,2,2], ‘sales’: [100.00, 200.00, 300.00, 400.00, 500.00, 100.00, 200.00, 300.00, 400.00], ‘year_month’: [202201, 202202, 0, 202204, 202205, 202201, 202202, 202203, 0]} df = pd.DataFrame(dic) Output: id sales year_month 0 1 100.0 202201 1 1 200.0 202202 2 …

Total answers: 2

pandas – ranking with tolerance?

pandas – ranking with tolerance? Question: Is there a way to rank values in a dataframe but considering a tolerance? Say I have the following values ex = pd.Series([16.52,19.95,16.15,22.77,20.53,19.96]) and if I ran rank: ex.rank(method=’average’) 0 2.0 1 3.0 2 1.0 3 6.0 4 5.0 5 4.0 dtype: float64 But what I’d like as a …

Total answers: 3

Pandas number rows within group in increasing order

Number rows within group in increasing order in a pandas dataframe Question: Given the following dataframe: import pandas as pd import numpy as np df=pd.DataFrame({‘A’:[‘A’,’A’,’A’,’B’,’B’,’B’], ‘B’:[‘a’,’a’,’b’,’a’,’a’,’a’], }) df A B 0 A a 1 A a 2 A b 3 B a 4 B a 5 B a I’d like to create column ‘C’, which …

Total answers: 3