Python - Pandas - 将数据帧写入 CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35881051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:49:59  来源:igfitidea点击:

Python - Pandas - Write Dataframe to CSV

pythoncsvpandasdataframe

提问by pHorseSpec

I'm trying to write a 4 table, 3 column, and 50 row dataframe file to a csv using pandas. I'm getting the following error AttributeError: 'dict' object has no attribute 'to_csv'. I believe I'm writing the syntax correctly, but could anyone point out where my syntax is incorrect in trying to write a dataframe to a csv?

我正在尝试使用 Pandas 将一个 4 表、3 列和 50 行的数据帧文件写入 csv。我收到以下错误AttributeError: 'dict' object has no attribute 'to_csv'。我相信我写的语法是正确的,但是有人能指出我的语法在尝试将数据帧写入 csv 时不正确的地方吗?

'dict' object has no attribute 'to_csv'

import pandas as pd
import numpy as np

df = pd.read_excel("filelocation.xlsx",
    sheetname=['pnl1 Data ','pnl2 Data','pnl3 Data','pnl4 Data'],
    skiprows=8, parse_cols="B:D", keep_default_na='FALSE', na_values=['NULL'])

df.to_csv('filelocation.csv', line_terminator=',', index=False, header=False) #error occurs on this line

回答by Daniel

Your intuition is right; there's nothing wrong with the syntax in your code.

你的直觉是对的;您的代码中的语法没有任何问题。

You are receiving the AttributeErrorbecause you are reading data from multiple sheets within your workbook, generating a dictionaryof DataFrames (instead of one DataFrame), from which you attempt to_csv(a method only available to a DataFrame).

您收到的是AttributeError因为您正在从工作簿中的多个工作表中读取数据,生成一个DataFrames字典(而不是一个 DataFrame),您从中尝试to_csv(一种仅适用于 DataFrame 的方法)。

As your code is written, the keys of the dictionary you generate correspond to the names of the worksheets, and the values are the respective DataFrames. It's all explained in the docs for the read_excel()method.

在编写代码时,您生成的字典的键对应于工作表的名称,值是相应的 DataFrame。这一切都在该read_excel()方法的文档中进行了解释。

To write a csv file containing the aggregate data from all the worksheets, you could loop through the worksheets and append each DataFrame to your file (this works if your sheets have the same structure and dimensions):

要编写一个包含来自所有工作表的汇总数据的 csv 文件,您可以遍历工作表并将每个 DataFrame 附加到您的文件(如果您的工作表具有相同的结构和尺寸,这将起作用):

import pandas as pd
import numpy as np

sheets = ['pnl1 Data ','pnl2 Data','pnl3 Data','pnl4 Data']

for sheet in sheets:
    df = pd.read_excel("filelocation.xlsx",
        sheetname=sheet,
        skiprows=8, 
        parse_cols="B:D", 
        keep_default_na='FALSE', 
        na_values=['NULL'])

    with open('filelocation.csv', 'a') as f:
        df.to_csv(f, line_terminator=',', index=False, header=False)