Reputation: 1998
I have a df with a column that I want to filter for only negative or only positve values,
when I try code below as:
df.loc[df['values'] > 0]
I get error of
`TypeError: '>' not supported between instances of 'str' and 'int'
I try to convert the object data type of the values column to integer:
df['values'].astype(str).astype(int)
I get error of : ValueError: invalid literal for int() with base 10: '3.69'
Thanks!
How Can I convert correctly so I can then filter correctly? Thanks!
Upvotes: 1
Views: 104
Reputation: 2583
If you want to convert it to int you should use apply function:
df = df.assign(values = lambda x: x['values'].apply(lambda s: int(s)))
Upvotes: 0
Reputation: 13447
You need to convert it to a float
dtype since 3.69 is a decimal (and therefore a float
). int
datatypes can only non-decimal numbers (e.g. 1, 2, 4, 100, 900). Try this:
df.loc[df['values'].astype(float) > 0]
Upvotes: 2