2

I put this dataframe as an example:

import pandas as pd df = pd.DataFrame({'country':['china','canda','usa' ], 'value':[1000, 850, 1100], 'fact':[1000,200,850]}) df.index=df['country'] df = df.drop('country', axis=1) 

I want to iterate over the GDP of each country and into this iteration I want to create a new column that would be full of 1 or 0 in function of a condition:

for x in df['value']: if x > 900: df['answer']=1 else: df['answer']=0 

I would expected a column with the following values:

[1,0,1] 

Because Canada has a value lower than 900.

But instead I have a column full of ones.

What is wrong?

1
  • Sorry, it is: I want to iterate over the value Commented Dec 2, 2019 at 7:03

2 Answers 2

1

Use np.where

df["answer"] = np.where(df["value"]> 900, 1,0) 

Or

df["answer"] = (df["value"]> 900).astype(int) 

Output:

 value fact answer country china 1000 1000 1 canda 850 200 0 usa 1100 850 1 

whats wrong with your code

When you do df['answer']=1, the expression assign 1 to all the rows in answer column.

So last evaluated value is assigned to that column

Sign up to request clarification or add additional context in comments.

1 Comment

@ilegionpy accept and upvote the answer if it helped
1

It can be even done without iterating over each row using:

df['answer'] = df['value'].apply(lambda value: 1 if value > 900 else 0) 

EDIT You are assigning df['answer'] to some value. The last value is 1 that is why it applies 1 to the entire answer column and not a particular row.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.