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