[英]Why is df.info().to_csv('File_name.csv') returning error?
I'm attempting to save df.info()
into a .csv
file.我正在尝试将df.info()
保存到.csv
文件中。
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
?将df.info()
保存到.csv
的任何想法?
df.info()
is a method that doesn't actually return anything. df.info()
是一种实际上不返回任何内容的方法。 Instead, (from the pandas docs) it "Prints a concise summary of a DataFrame."相反,(来自 pandas 文档)它“打印 DataFrame 的简明摘要。” This means that you are running df.info()
(which returns None
) and then trying to call to_csv()
on that.这意味着您正在运行df.info()
(返回None
),然后尝试调用to_csv()
。
Saving df.info()
as a csv will be difficult, given the output is actually not a table.将df.info()
保存为 csv 将很困难,因为 output 实际上不是表。 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:您可以将整个 output 保存到文本文件中,如 pandas 文档中所述:
with open('info.txt', 'w') as f:
df.info(buf=f)
df.info()
prints a summary of the dataframe and returns None
. df.info()
打印 dataframe 的摘要并返回None
。 And that doesn't have any method to_csv
.这没有任何方法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)
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. 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如果您希望将其写入 CSV,您可以替换with open("df_info.txt", "w",encoding="utf-8") as f
With with open("df_info.csv", "w",encoding="utf-8") as f
with open("df_info.txt", "w",encoding="utf-8") as f
with with open("df_info.csv", "w",encoding="utf-8") as f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.