Stack Overflow Asked by sshaikh on December 30, 2021
I’m attempting to save df.info()
into a .csv
file.
I tried using this:
df.info().to_csv('file_name.csv')
However receive this error:
AttributeError: 'NoneType' object has no attribute 'to_csv'
Any ideas for saving the df.info()
to a .csv
?
import io
buffer = io.StringIO()
df.info(buf=buffer)
s = buffer.getvalue()
with open("df_info.txt", "w",encoding="utf-8") as f:
f.write(s)
According to the pandas documentation https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.info.html this is how you can write df.info to a file.
If you wish to write it to a CSV, you can replace the
with open("df_info.txt", "w",encoding="utf-8") as f
With
with open("df_info.csv", "w",encoding="utf-8") as f
Answered by Harshwardhan Nandedkar on December 30, 2021
df.info()
prints a summary of the dataframe and returns None
. And that doesn't have any method to_csv
.
But from you question I guess you are looking fro something like this:
with open('output.txt','w') as out:
df.info(buf = out)
Answered by Sai Sreenivas on December 30, 2021
df.info()
is a method that doesn't actually return anything. Instead, (from the pandas docs) it "Prints a concise summary of a DataFrame." This means that you are running df.info()
(which returns None
) and then trying to call to_csv()
on that.
Saving df.info()
as a csv will be difficult, given the output is actually not a table. It contains a table, but also contains info such as
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000000 entries, 0 to 999999
Data columns (total 3 columns):
and
dtypes: object(3)
memory usage: 22.9+ MB
You can save the entire output to a text file as mentioned in the pandas documentation:
with open('info.txt', 'w') as f:
df.info(buf=f)
Answered by Thomas on December 30, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP