This drives me nuts. When I searched for tips about dropping elements in a dataframe there was nothing about mixed typed series.
Say here is a dataframe:
import pandas as pd
df = pd.DataFrame(data={'col1': [1,2,3,4,'apple','apple'], 'col2': [3,4,5,6,7,8]})
a = df['col1']
Then 'a' is a mixed typed series with 6 components. How can I remove all 'apple's from a? I need series = 1,2,3,4.
To retain the integers as integer type without changing them to float:
Approach: filter rows with numeric values to keep (instead of converting non-numeric values to
NaN
then dropNaN
). The difference is that we won't have intermediate result withNaN
, which will force the numeric values to change from integer to float.Result:
The resulting dtype remains as integer type
int64
If you produce intermediate results with
NaN
like below:The resulting dtype is forced to change to
float
type (instead of remaining as integer)