Reputation: 2495
I have a dataframe contains a tuple of (lat,lon), but there are some row contain a tuple of nan. How can I remove or detect these?
Example:
import pandas as pd
import numpy as np
df = pd.DataFrame(data={'location': [1, 2, 3],
'coor': [(14.48847, 103.161477),
(14.970084, 103.062853),
(np.nan, np.nan)]})
location coor
0 A (14.48847, 103.161477)
1 B (14.970084, 103.062853)
2 C (nan, nan)
I tried.
df.isna()
location coor
0 False False
1 False False
2 False False
df.dropna()
location coor
0 A (14.48847, 103.161477)
1 B (14.970084, 103.062853)
2 C (nan, nan)
But it isn't work at all. Should i split a tuple into 2 columns or there is a way to deal with thses? Any help or guide would be appreciated.
Upvotes: 0
Views: 960
Reputation: 323326
Without apply will speed up
df[pd.DataFrame(df.coor.tolist()).notna().all(1)]
Out[361]:
coor location
0 (14.48847, 103.161477) 1
1 (14.970084, 103.062853) 2
Upvotes: 3
Reputation: 28303
you could try filtering using a lambda function
df[~df.coor.apply(lambda x: np.isnan(x[0]) & np.isnan(x[1]))]
# prints
location coor
0 1 (14.48847, 103.161477)
1 2 (14.970084, 103.062853)
Upvotes: 1