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

Pandas values not being updated

I’m new to working with Pandas and I’m trying to do a very simple thing with it. Using the flights.csv file I’m defining a new column which defines a new column with underperforming if the number of passengers is below average, the value is 1. My problem is that it might be something wrong with the logic since it’s not updating the values. Here is an example:

df = pd.read_csv('flights.csv')
 
passengers_mean = df['passengers'].mean()
df['underperforming'] = 0

for idx, row in df.iterrows():
    if (row['passengers'] < passengers_mean):
        row['underperforming'] = 1


print(df)
print(passengers_mean)

Any clue?

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

>Solution :

According to the docs:

You should never modify something you are iterating over. This is not guaranteed to work in all cases.

iterrows docs

What you can do instead is:

df["underperforming"] = (df.passengers < x.passengers.mean()).astype('int')
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