0

There is a very similar question to the one I am about to ask posted here:

Reading an Excel file in python using pandas

Except when I attempt to use the solutions posted here I am countered with

AttributeError: 'DataFrame' object has no attribute 'read'

All I want to do is convert this excel sheet into the pandas format so that I can preform data analysis on some of the subjects of my table. I am super new to this so any information, advice, feedback or whatever that anybody could toss my way would be greatly appreciated.

Heres my code:

import pandas
file = pandas.read_csv('FILENAME.csv', 'rb')
# reads specified file name from my computer in Pandas format
print file.read()

By the way, I also tried running the same query with file = pandas.read_excel('FILENAME.csv', 'rb') returning the same error.

Finally, when I try to resave the file as a .xlsx I am unable to open the document.

Cheers!

Community
  • 1
  • 1
Justin T
  • 47
  • 1
  • 1
  • 7
  • `file` is already a `DataFrame` pandas object. – juanpa.arrivillaga Jul 14 '16 at 22:10
  • OK thanks, does that mean I should just drop the whole 'file =' ? – Justin T Jul 14 '16 at 22:12
  • Well, `file` isn't a good name for the variable, but that doesn't really matter. Regardless, you need to have a variable or else you won't be able to access the data. – juanpa.arrivillaga Jul 14 '16 at 22:14
  • Just change `file =` to `df =` (a common but not necessary naming convention) and try, for example, `df[:5]` – Jeff Jul 14 '16 at 22:21
  • Thanks Jeff I just tried that edit and its still returning the same error: TypeError: descriptor 'read' of 'file' object needs an argument – Justin T Jul 14 '16 at 22:24
  • Also, if you are working with csv's you probably won't need anything other than the `pandas.read_csv` function to read your data into a `DataFrame`. The reason you should avoid `file` as a name for one of your variables is that the `file` identifier is already used in python 2 (although not in 3) for the built-in `file` function, and you shouldn't overwrite names of built-ins. – juanpa.arrivillaga Jul 14 '16 at 22:25
  • **Don't** use file.read(). There is no need for it. – juanpa.arrivillaga Jul 14 '16 at 22:27
  • OK great, I got rid of the file.read() and it came back with no errors but now its not showing the resulting table. Any advice on showing the table? – Justin T Jul 14 '16 at 22:30
  • Never mind I figured that one out! – Justin T Jul 14 '16 at 22:33

2 Answers2

1

read_csv() return a dataframe by itself so there is no need to convert it, just save it into dataframe.

I think this should work

import pandas as pd #It is best practice to import package with as a short name. Makes it easier to reference later. 
file = pd.read_csv('FILENAME.csv')
print (file)
Tashay Green
  • 75
  • 3
  • 11
Ajay Sant
  • 665
  • 1
  • 11
  • 21
0

Your error message means exactly what it says: AttributeError: 'DataFrame' object has no attribute 'read'

When you use pandas.read_csv you're actually reading the csv file into a dataframe. BTW, you don't need the 'rb'

df = pandas.read_csv('FILENAME.csv')

You can print (df) but you can not do print(df.read()) because the dataframe object doesn't have a .read() attribute. This is what's causing your error.

Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48