'How to filter dataframe on basis of column datatype
Let's say I have following dataframe, and want to filter/separate dataframe based on datatype of column value,
dataframe =
Name | No_of_days
A | 23
B | 34
C | 'not applicable'
D | 'present'
E | 12
F | 'something'
expected1 =
Name | No_of_days
A | 23
B | 34
E | 12
expected2 =
Name | No_of_days
C | 'not applicable'
D | 'present'
F | 'something'
I want to filter dataframe by column(No_of_days), where datatype is integer and string.
Solution 1:[1]
Use to_numeric with errors='coerce' - for not numeric are generated missing values, so possible test by Series.isna and filter in boolean indexing:
m = pd.to_numeric(df['No_of_days'], errors='coerce').isna()
expected1 = df[m]
expected2 = df[~m]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
