Reputation: 134
Trying to write a For loop that looks to see if a number exists in the Pay_Day_Supply_Count
column and if so, replace the day number in the Days
column with a 0
.
Example Dataframe
|Days | Pay_Day_Supply_Count |
--------------------------------|
| 0 | 15 |
| -456 | 50 |
| 121 | 10 |
| 0 | 15 |
| 0 | 10 |
| 323 | 20 |
| 0 | 05 |
| -444 | 10 |
| 141 | 20 |
---------------------------------
Expected Output
|Days | Pay_Day_Supply_Count |
--------------------------------|
| 0 | 15 |
| 0 | 50 |
| 0 | 10 |
| 0 | 15 |
| 0 | 10 |
| 0 | 20 |
| 0 | 05 |
| 0 | 10 |
| 0 | 20 |
---------------------------------
This is what i've tried so far:
for i, p in enumerate(df["Days"]):
if df["PAY_DAY_SUPPLY_CNT"] != '':
p1 = p.replace('%d', 0)
df.at[i, 'Days'] = p1
Returned error
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Upvotes: 0
Views: 2090
Reputation: 5437
Maybe using boolean masking could help. Unsure about your dtypes though:
mask = df[Pay_Day_Supply_Count].notna()
df.loc[mask, "Day"]=0
Upvotes: 3
Reputation: 3391
You can use numpy.where
function to do this in one line :
import pandas as pd
import numpy as np
df['Days'] = np.where(df["PAY_DAY_SUPPLY_CNT"] != '', 0 , df['Days'] )
Upvotes: 1