I am trying to loop through a pandas data frame and replace values in certain columns if they meet certain conditions. I realize there are more straightforward ways to do this in general, but in my specific example I need a loop because the result for one row can depend on the prior row. Below is a reproducible example of what is going wrong. When I try to replace text it does not replace it.
import pandas as pd
df = pd.DataFrame({"A": ["I", "AM", "NOT", "WORKING", "!"], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]})
for index, row in df.iterrows():
row['A'] = "I am working!"
print(df)
Which prints:
A B C
0 I 20 32
1 AM 30 234
2 NOT 10 23
3 WORKING 40 23
4 ! 50 42523
Pandas DataFrame replace() MethodThe replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
You can write to the original frame using .loc
:
>>> for index, row in df.iterrows():
... df.loc[index, "A"] = "I am working! {}".format(row["B"])
...
>>> df
A B C
0 I am working! 20 20 32
1 I am working! 30 30 234
2 I am working! 10 10 23
3 I am working! 40 40 23
4 I am working! 50 50 42523
[5 rows x 3 columns]
Aside: even if one row depends upon the previous row there can be ways to vectorize it, but I admit sometimes it's much simpler to do it the manual, loop-based way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With