Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace values in Pandas data frame within a loop

Tags:

python

pandas

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
like image 842
Michael Avatar asked Jan 26 '14 18:01

Michael


People also ask

How do you replace something in a data frame?

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.

How do you replace values in a DataFrame based on a condition?

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.


1 Answers

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.

like image 96
DSM Avatar answered Sep 18 '22 22:09

DSM