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

Change value based on condition on slice of dataframe

I have a dataframe like this:

df = pd.DataFrame(columns=['Dog', 'Small', 'Adult'])
df.Dog = ['Poodle', 'Shepard', 'Bird dog','St.Bernard']
df.Small = [1,1,0,0]
df.Adult = 0

That will look like this:

       Dog         Small   Adult
0      Poodle          1   0
1      Shepard         1   0
2      Bird dog        0   0
3      St.Bernard      0   0

Then I would like to change one column based on another. I can do that:

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.loc[df.Small == 0, 'Adult'] = 1

However, I just want to do so for the 3 first rows.

I can select the first three rows:

df.iloc[0:2]

But if I try to change values on the first three rows:

df.iloc[0:2, df.Small == 0, 'Adult'] = 1

I get an error.

I also get an error if I merge the two:

df.iloc[0:2].loc[df.Small == 0, 'Adult'] = 1

It tells me that I am trying to set a value on a copy of a slice.

How should I do this correctly?

>Solution :

You could include the range as another condition in your .loc selection (for the general case, I’ll explicitly include the 0):

df.loc[(df.Small == 0) & (0 <= df.index) & (df.index <= 2), 'Adult'] = 1

Another option is to transform the index into a series to use pd.Series.between:

df.loc[(df.Small == 0) & (df.index.to_series().between(0, 2)), 'Adult'] = 1
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