Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can you help me, please?

>Solution :

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'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading