0

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.

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!

Judah
  • 37
  • 5

1 Answers1

1

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).

  • Thank you! This worked. I'm not sure why the file exported with extra spaces (don't think I put them in myself) but will use this method when this happens again – Judah Nov 26 '21 at 23:46
  • Yeah, it happens sometimes. It might be because there are spaces in the Excel spreadsheet? Anyway, remember to accept this answer as soon as you can :) –  Nov 26 '21 at 23:47