Home > database >  Add a column (in Pandas) that is calculated based on another column
Add a column (in Pandas) that is calculated based on another column

Time:12-02

I have a simple database that has every month's earnings, with Year (values 1991-2020), Month (Jan-Dec) and Earnings. I want to make a new column, where for years 1991-2005 I divide the Earnings column by 10000 but for 2006-2020 I want it to be the same as in the earnings column.

I am a beginner, but what I was thinking is that I want the new column (TrueEarn) to be Earnings/10000 but only for columns 1991-2005.

df['TrueEarn'] = df['Earnings']/10000 for (['Year']=('1991':"2005"))

Since I am a newb with Python, this may not make sense for you, but that is how I logically wanted to write it

Can you help me, please?

CodePudding user response:

Yoy should provide a minimum reproducible example. But assuming that you have the year in another column, the way to go could be

df_imp['TrueEarn'] = np.where((df['YEAR'] >= 1991) & (df['YEAR'] <= 2005),
                               df['Earnings'] / 10000, df['Earnings'])
  • Related