Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Pandas Column Names

I have a CSV file with four columns: Category, Type, Provider, and Cost.

Category ,Type,Provider,Cost 
Home,Mortgage,Real Homes,"$1,500.00"
Utilities,Gas,Landsberg Gas,$100.00
Utilities,Electric,Edison,$100.00
Utilities,Internet,Verizon Fios,$60.00
Food,Groceries,Ralphs,$280.00

I imported the csv file into a DataFrame using the following code:

bills = pd.read_csv('Bills.csv')

I then tried to read the column names in the terminal, e.g., bills.Category.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I found that bills.Category and bills.Cost could not be read and returned attribute errors. I modified the CSV file so that Category became mainCategory and Cost became Price. After revising, I re-ran the method above and the columns were read and printed correctly to the terminal.

I could not figure out why I was receiving these errors the first time, other than there being some keywords that are restricted in Python/Pandas.

Any explanation of this would be really helpful!

>Solution :

Your CSV file has spaces around some of the column names, which is why you are having errors. Right after you call pd.read_csv, add this line:

bills.columns = bills.columns.str.strip()

Then you should be able to do bills.Category etc. (if you change mainCategory back to Category).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading