Reputation: 13914
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
Upvotes: 10
Views: 28777
Reputation: 352989
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.
Upvotes: 18