Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas DataFrame: unorderable types: str() > int()

I have downloaded the file mentioned in book "Python for Data Analysis" and was going through the example FEC database mentioned in page 278. I get the following Type error when I ran the command. My versions: Python 3.4; Pandas: 0.14.0. OS: Windows 8

>>> fec=pd.read_csv('c:\python\P00000001-ALL.csv')
>>> (fec.contb_receipt_amt > 0).value_counts()
>>> TypeError: unorderable types: str() > int()

But it is not just this dataset. Any dataset that I am working with have similar problem. Int(Number) data types are being imported as objects like anything else and when run any comparison with numbers(>0) on them I get the above error. What is the work around? I tried importing with dtype option, which throws an error that says int64 or Float64 is not available. I am sure there is a right way of doing. How to load the data frame with the right data types.

Any help is appreciated.

like image 970
user3770271 Avatar asked Jun 24 '14 08:06

user3770271


People also ask

How do I change data type to STR in pandas?

Use pandas DataFrame. astype() function to convert a column from int to string, you can apply this on a specific column or on an entire DataFrame.

Can pandas DataFrame store different data types?

A DataFrame is a 2-dimensional data structure that can store data of different types (including characters, integers, floating point values, categorical data and more) in columns.


1 Answers

I got this error for some not identical dates. I solved it using a type change first.

Try :

fec[[contb_receipt_amt]] = fec[[contb_receipt_amt]].astype(str)

Then try again the count.

like image 147
Denis Cottin Avatar answered Sep 21 '22 21:09

Denis Cottin