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 cant to 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? Thank you.
I found the answer:
m1 = (test4['odometer'] > 30000) & (test4['odometer'] <= 150000)
m2 = (test4['odometer'] <= 30000) | (test4['odometer'] > 150000)
test4.loc[m1,'condition'] = test4.loc[m1,'condition'].fillna('excellent')
test4.loc[m2,'condition'] = test4.loc[m2,'condition'].fillna("good")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.