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

How to update an existing column of a dataframe based on values in another column?

In python, I’m dealing with a sales data of about 21,000 houses. Each house (row) has columns ‘sqft_living’ (int64), and ‘grade’ (int64) among others.
I want to increase the ‘grade’ of every house by 2 if its sqft_living > 400, or by 1 otherwise.
How do I do this?

>Solution :

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

df = pd.DataFrame()
df['sqft_living'] = [x for x in range(396, 405)]
df['grade'] = [x for x in range(9)]
df

So we have:

sqft_living     grade
0   396     0
1   397     1
2   398     2
3   399     3
4   400     4
5   401     5
6   402     6
7   403     7
8   404     8

We apply the condition:

df['grade'] = df.apply( lambda row: row.grade + 1 if row.sqft_living < 400 else row.grade + 2 , axis=1)
df

New dataframe:

sqft_living     grade
0   396     1
1   397     2
2   398     3
3   399     4
4   400     6
5   401     7
6   402     8
7   403     9
8   404     10
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