Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185554
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T01:11:06+00:00 2022-06-10T01:11:06+00:00

python – Pandas Multi Index Dataframe Transform Raw Score into Quantile by Date and add Percentile Column

  • 0

[ad_1]

I have a pandas multi index that looks like this:

enter image description here

I would like to transform the Raw_Score into a percentile, where the comparison is against the day. So the top score of the day gets a 100, and the bottom score of the day gets at zero, with the other scores proportionately aligned. I would like to do this at the “all data”, “Sector”, or “Region” level. The dataframe would have a new column called “Adjusted_Score”. I think I have to use groupby and quantile, but I am lost. Can someone point me in the right direction?

Here is my code to set up the df:

import pandas as pd
import numpy as np

from datetime import datetime
from numpy import random

def create_df(num):
  # create empty data frame in pandas
  df = pd.DataFrame()

  # add a range of dates
  dates = pd.date_range(start="2022-04-01",end="2022-06-05").to_pydatetime().tolist()
  df['Dates'] = dates

  # generate a random 3 digit Value
  #for i in range(len(dates)):
  df['Raw_Score'] = np.random.randint(-999, 999, size=len(df))

  # generate a random 2 digit Sector between 10 and 15
  #for i in range(len(dates)):
  df['Sector'] = np.random.randint(10, 20, size=len(df))

  # give it a unique value
  df['Region'] = num

  return df

# make a big df
big_df = []

for num in range(10):
  df = create_df(num)
  df = pd.DataFrame(df)

  while num == 0:
    big_df = df.copy()
    num = num + 1
  else:
    big_df = pd.concat((big_df, df), axis=0)
  
big_df.set_index(["Region", "Sector", "Dates"])

big_df.groupby(level=[0,1]).quantile()

Thank you!

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 325 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 295 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 292 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.