transform a dataframe to list ans delete NaN values
hi I have the following dataset
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'NaN', 'mango'],
'price': ['40', '80', 'NaN', '40', '30', '80']
})
fruits price
0 orange 40
1 mango 80
2 apple NaN
3 grapes 40
4 NaN 30
I want to return a list that does not have the NaN values. So I am using the following code:
dfd= [[y for y in x if pd.notna(y)] for x in df.values.tolist()]
However, the NaN values are still there
[['orange', '40'],
['mango', '80'],
['apple', 'NaN'],
['grapes', '40'],
['NaN', '30'],
['mango', '80']]
Any ideas?
Because NaN
is string pd.notna
return False
, if need remove 'NaN's
strings use:
dfd= [[y for y in x if Y != 'NaN'] for x in df.values.tolist()]
If convert NaN
s strings to missing values:
df = df.replace('NaN', np.nan)
dfd= [[y for y in x if pd.notna(y)] for x in df.values.tolist()]