Reputation: 55
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.
Upvotes: 0
Views: 83
Reputation: 1738
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)
Upvotes: 1
Reputation: 8816
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
Upvotes: 1
Reputation: 1661
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"})
Upvotes: 0
Reputation: 13255
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
Upvotes: 2