I have this pd.DataFrame:
test4 = pd.DataFrame({'condition': ['good', np.nan, np.nan, 'excellent', 'good', np.nan],
'odometer': [np.nan, 35000, 20000, 100000, 500000, 50]})
Output:
condition odometer
0 good NaN
1 NaN 35000.0
2 NaN 20000.0
3 excellent 100000.0
4 good 500000.0
5 NaN 50.0
And I can't fill the NaN values in the column "condition" using the values in the column "odometer". The conditions would be:
odometer <=30000, then condition = 'good'
odometer > 30000 & odometer <=150000, then value = 'excellent'
odometer > 150000 & odometer <=10000000, then value = 'good'
And it should look like this in the end:
condition odometer
0 good NaN
1 excellent 35000.0
2 good 20000.0
3 excellent 100000.0
4 good 500000.0
5 good 50.0
I have tried different stuff but none worked. For example:
def f(change):
if change['condition'] == np.nan:
condition = change['odometer']
value = change['condition']
if condition <=30000 value = 'good'
elif condition > 30000 & condition <=150000 value = 'excellent'
elif condition > 150000 & condition <=10000000 value = 'good'
return value
return change['condition']
test4['condition'] = test4.apply(f)
What am I doing wrong? Is there a way to make it work?
I found the answer: