Pandas read_csv error while using to_dict - python

Previously i was using python DictReader to read and load each row in python dict, But now due to some design changes i have to use pandas to do the same. But while trying to achieve same behaviour i am using this code
for idx,row_r in enumerate(pd.read_csv(input_file_path,chunksize=10000, skiprows=skip_n_rows).to_dict()):
But i am getting this error
AttributeError: 'TextFileReader' object has no attribute 'to_dict'
python 3.7.6
pandas 0.23.0
P.S i am using chunksize because CSV file is large.

Related

Pandas read_xml - AttributeError: module 'pandas' has no attribute 'read_xml'

I ran into the error:
" AttributeError: module 'pandas' has no attribute 'read_xml' "
This would be a huge lifesaver if I could ingest the XML with one function into a pandas df without trying to iterate through etc.
I am running pandas 1.3.4 and python 3.8.8. I have tried opening an xml in my local folder (where the script is housed).
I tried directly importing the file like so:
df = pd.read_xml('xmltest.xml')
As well as trying to import via a string like so:
txt = Path('xmltest.txt').read_text()
df = pd.read_xml(txt)
And both gave me the wrong error.. Any help would be awesome as this would be AMAZING to ingest XML with 1 function into a DF!!! Are there similar functions out there if this is no longer a valid solution?
It appears this person had the same problem but I'm currently running the updated pandas:
AttributeError: module 'pandas' has no attribute 'read_xml' or 'to_xml'

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

I'm trying to save a pandas DataFrame in binary data formats and book says that pandas objects all have save method which writes the data to disc as a pickle. but when I run the code there is an error. Is there save method for pandas objects in pandas new versions? I'm using pandas 0.25.3
import pandas as pd
frame = pd.read_csv('PandasTest.csv')
frame.save('PandasTest_Pickle')
The error is:
AttributeError: 'DataFrame' object has no attribute 'save'
As others in comment section suggested, use 'to_pickle' and 'read_pickle' methods. For e.g,
import pandas as pd
frame=pd.read_csv('data.csv')
frame.to_pickle('frame_pickle')
pd.read_pickle('frame_pickle')

what is the correct way to read a csv file into a pandas dataframe?

I am doing a data analysis project and while importing the csv file into spyder I am facing this error. Please help me to debug this as I am new to programming.
#import library
>>>import pandas as pd
#read the data from from csv as a pandas dataframe
>>>df = pd.read.csv('/Documents/Melbourne_housing_FULL.csv')
This is the error shown when I use the pd.read.csv command:
File "C:/Users/mylaptop/.spyder-py3/temp.py", line 4, in <module>
df = pd.read.csv('/Documents/Melbourne_housing_FULL.csv')
AttributeError: module 'pandas' has no attribute 'read'
you should use :
df = pd.read_csv('/Documents/Melbourne_housing_FULL.csv')
see here docs
you need to use pandas.read_csv() instead of pandas.read.csv() the error is litterally telling you this method doesn't exist .

Python Pandas Error: AttributeError: 'module' object has no attribute 'formats'

I have 0.20.3 version of pandas install. I am trying to set header_style to false so that i can format the header row. xlsxwriter not applying format to header row of dataframe - Python Pandas
I keep getting error : AttributeError: 'module' object has no attribute 'formats'
I have tried
pd.formats.format.header_style = None
and
pd.core.format.header_style = None
Any idea what am I doing wrong ?
As you can see in the API, the module pandas.formats and pandas.core.format do not exist : https://pandas.pydata.org/pandas-docs/stable/api.html
It is normal that you have this error.
If you read new API changes with 0.20, pandas.formats has become pandas.io.formats. Try to check the API.
Another way to do this, suggested by #Martin Evans, is to write the headers directly, outside of Pandas. This avoids issues like above with different Pandas versions.
See also this example in the XlsxWriter docs.

Reading an excel data set saved as CSV file in pandas

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

Categories