Skip to content
Advertisement

Pandas: filter dataframe with type of data

I have dataframe. It’s a part

        member_id event_duration             domain           category
0          299819             17  element.yandex.ru               None
1          299819              0        mozilla.org          Программы
2          299819              4          vbmail.ru               None
3          299819              aaa          vbmail.ru               None

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:

In [47]:
df[pd.to_numeric(df['event_duration'], errors='coerce').notnull()]

Out[47]:
   member_id event_duration             domain   category
0     299819             17  element.yandex.ru       None
1     299819              0        mozilla.org  Программы
2     299819              4          vbmail.ru       None

This:

df[df.event_duration.astype(int) == True]

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