Replacing value related to a specific date in df?

Question:

I have a df like as follows

          Date               Flow
0   1981-01-01  103.432860
1   1981-01-02  102.982800
2   1981-01-03  102.121150
3   1981-01-04  100.92662
...     ....
xx      2020-12-31      150.123

I need to replace the value of flow for every 1st of january as 0.00

df['month'] = df['Date'].dt.month_name()
df['dom']= df['Date'].dt.day

for ind in df.index: 
    df.loc[(df['month'] == 'January') & (df['dom'] == '01'), 'Flow'] = 0

df.head()
Results:
          Date            Flow           month         dom
0   1981-01-01  103.432860  January     1
1   1981-01-02  102.982800  January         2
2   1981-01-03  102.121150  January         3
3   1981-01-04  100.926620  January         4

It’s not working

Asked By: Fain

||

Answers:

Use an int rather than a string

As MattDMo pointed out, the reason you code is failing is because you are filtering by df["dom"] == "01" instead of df["dom"] == 0. Because the dom is expressed in integers rather than strings, it is failing to match any columns.

A suggestion

Rather than looping through each line of the dataframe, select just the lines you want to change using loc.

df.loc[(df["month"] == "January") & (df["dom"] == 1), "Flow"] = 0

Note that you need to encase each filter in parentheses to allow for column logic, as you have done.

Answered By: Keverly