Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: filter dataframe with type of data

Tags:

python

pandas

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]?

like image 775
Petr Petrov Avatar asked Dec 23 '22 22:12

Petr Petrov


1 Answers

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

like image 113
EdChum Avatar answered Jan 08 '23 23:01

EdChum