Skip to content
Advertisement

Pandas: filter dataframe with type of data

I have dataframe. It’s a part

JavaScript

How filter df with type? Usually I do it with str.contains, maybe it’s normal to specify any like df[df.event_duration.astype(int) == True]?

Advertisement

Answer

If all the other row values are valid as in they are not NaN, then you can convert the column to numeric using to_numeric, this will convert strings to NaN, you can then filter these out using notnull:

JavaScript

This:

JavaScript

won’t work as the string will raise an ValueError exception as the string cannot be converted

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement