-1

I am trying to fill the missing values using if-else condition but getting the value error How can I resolve? I read another similar post but couldn't apply to my problem

I have tried if-else (value error) and also, iterate through indexing using for loop

When using for loop, it runs all rows through only if statement, and not going to else

Only if else statement(Value error)

if data['Waiting Time'] > 0:
    data['Existing_Date'].fillna(data['New_time'],inplace=True) #if wait_time > 0         
else:
    data['Existing_Date'].fillna(data['Actual Date'],inplace=True) #if wait_time > 0

Using For-loop indexing(only running if statement )

for i in data.index:
    if data['Waiting Time'].iloc[i] > 0:
        data['Existing_Date'].fillna(data['New_time'],inplace=True) #if wait_time > 0    
    else:
        data['Existing_Date'].fillna(data['Actual Date'],inplace=True) #if wait_time = 0

With if only statement Value error

with for loop: only result in if statement, not going to else statement

How can I solve this? Thanks

Rick707
  • 63
  • 1
  • 5
  • Would you mind sharing the TraceBack for the error ? – realr Aug 03 '19 at 03:46
  • Can you give a small example of what the `data` looks like before, and how it should change after? – Karl Knechtel Aug 03 '19 at 03:52
  • @KarlKnechtel data['Exisitng_Date'] column has missing value I want to fill in with New_time column if df (waiting time - equal rows has integers) is greater than 0 else with Actual Date coumn type is Datetime64 – Rick707 Aug 03 '19 at 04:01
  • Does https://stackoverflow.com/questions/27041724/using-conditional-to-generate-new-column-in-pandas-dataframe help? – Karl Knechtel Aug 03 '19 at 04:25

1 Answers1

0

The reason why the first code does not enter else: is because you are testing the entire dataframe, and it will return True for existence. You are not actually querying the values that meet the condition.

The reason why you are getting the ValueError is because you are testing multiple values at the same time (the entire array of values), in which case you potentially get multiple True and multiple False.

I would not recommend looping through a dataframe like you did. You can use loc and some logic. It really depends on the exact output you are looking for, but likely pandas have already tools for your use without the need to iterate through the dataframe

data.loc[data['Waiting Time'] > 0,'Existing_Date'].fillna(data['New_time'],inplace=True)
data.loc[data['Waiting Time'] <= 0,'Existing_Date'].fillna(data['Actual Date'],inplace=True)
realr
  • 3,652
  • 6
  • 23
  • 34
  • I tried this, it is showing me 'NaT' values in missing fields after running – Rick707 Aug 03 '19 at 04:03
  • Is your data as `datetime` or as `str` ? Per the docs: _For datetime64[ns] types, NaT represents missing values. This is a pseudo-native sentinel value that can be represented by NumPy in a singular dtype (datetime64[ns]). pandas objects provide compatibility between NaT and NaN._ – realr Aug 03 '19 at 04:12