0

I have the following Dataframe object df:

    A        B         C       
0  0.0      5.0       0.0        
1  0.0      6.0       0.0        
2  0.0      9.0       0.0        
3  0.0      0.0       0.0        
4  0.0      2.0       0.0        
5  0.0      5.0       0.0        
6  6.0      0.0       0.0        
7  0.0      0.0       0.0        
8  0.0      1.0       0.0        

I want to change the values of column 'B':

If the value is smaller than 3, than the value should be replaced with 'False'.

Otherwise the value should be replaced with 'True'.

I tried:

df['B'] = df['B'].apply(lambda x: [False if y < 3 else True for y in x])

In this case I get the TypeError: 'float' object is not iterable.

When I use it for the whole Dataframe it works though:

df = df.apply(lambda x: [False if y < 3 else True for y in x])

Any help will be appreciated.

4 Answers 4

2

Use direct comparison or ge as:

df['B'] = df['B']>=3

OR

df['B'] = df['B'].ge(3)

print(df)

     A      B    C
0  0.0   True  0.0
1  0.0   True  0.0
2  0.0   True  0.0
3  0.0  False  0.0
4  0.0  False  0.0
5  0.0   True  0.0
6  6.0  False  0.0
7  0.0  False  0.0
8  0.0  False  0.0
Sign up to request clarification or add additional context in comments.

Comments

1

simply as:

>>> import pandas as pd
>>> df = pd.DataFrame([1,2,3,4], columns=["data"])
>>> df["B"] = df["data"] > 2
>>> df
   data      B
0     1  False
1     2  False
2     3   True
3     4   True
>>>

You can even include numpy for conditional comparison like below:

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame([1,2,3,4], columns=["A"])
>>> df["B"] = np.where(df["A"] <2, "False", "True")
>>> df
   A      B
0  1  False
1  2   True
2  3   True
3  4   True

Comments

1

Below code may help your approach, when apply method get used in column then it gets all the values of that column so i don't think to use for loop here

df['b'] = df['b'].apply(lambda x: True if x>3 else False)

Comments

0
df.loc[df.B > 3, 'new'] = True
df.loc[df.B < 3,'new'] = False
df = df.drop("B", axis=1)
df = df.rename(columns={"new": "B"})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.