I have dataframe. It’s a part
JavaScript
x
6
1
member_id event_duration domain category
2
0 299819 17 element.yandex.ru None
3
1 299819 0 mozilla.org Программы
4
2 299819 4 vbmail.ru None
5
3 299819 aaa vbmail.ru None
6
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
1
9
1
In [47]:
2
df[pd.to_numeric(df['event_duration'], errors='coerce').notnull()]
3
4
Out[47]:
5
member_id event_duration domain category
6
0 299819 17 element.yandex.ru None
7
1 299819 0 mozilla.org Программы
8
2 299819 4 vbmail.ru None
9
This:
JavaScript
1
2
1
df[df.event_duration.astype(int) == True]
2
won’t work as the string will raise an ValueError
exception as the string cannot be converted