self.df['X'] = self.df['x'].apply(lambda x: my_map.get(x))
How can i a drop those rows where my_map.get(x)
returns None
.
I am looking for a solution where i do not have to iterate over the column again to drop rows.
Thanks
self.df['X'] = self.df['x'].apply(lambda x: my_map.get(x))
How can i a drop those rows where my_map.get(x)
returns None
.
I am looking for a solution where i do not have to iterate over the column again to drop rows.
Thanks
I think you need dropna
, because is possible remove None
in first step, by assign to new column create NaN
s:
self.df['X'] = self.df['x'].apply(lambda x: my_map.get(x))
self.df = self.df.dropna('X')
Or:
self.df = self.df[self.df['X'].notnull()]
Either loc
or pd.Series.compress
take a callable argument and return a subset where the callable evaluates to True
compress
self.df['x'].compress(lambda x: my_map.get(x) is not None)
loc
self.df['x'].loc[lambda x: my_map.get(x) is not None]
You can find the indices as follows
idxs = self.df.index[self.df['X'].isnull()] # find all indices with None in df.X
Full code:
self.df['X'] = self.df['x'].apply(lambda x: my_map.get(x))
idxs = self.df.index[self.df['X'].isnull()] # find all indices with None in df.X
self.df = self.df.drop(idxs)
You can do this as a merge, if you convert your mymap
to a dict:
mymerge = pd.DataFrame.from_dict(mymap, orient = 'index')
Then use a left join, to only join on the required columns:
mymerge.merge(df, left_index = True, right_on = 'x')
In one line:
pd.DataFrame.from_dict(mymap, orient = 'index').merge(df, left_index = True, right_on = 'x')