【问题标题】:What data structure is returned by Pands.read_excel, and how can I reference the columns of the underlying data frames?Pands.read_excel 返回什么数据结构,如何引用底层数据框的列?
【发布时间】:2015-09-09 01:19:13
【问题描述】:

我正在尝试使用在 Python 3.4.3 的 Ipython 环境中运行的 Pandas 来绘制列。使用read_excel 函数,我尝试将 xls 转换为 DataFrame,如下所示:

import matplotlib.pyplot as plt  
import pandas as pd  

data=pd.read_excel('/Path/to/file.xlsx',sheetname='Sheet1')
print(sup_sub)

导致

{'Sheet1':          Day   a   b     c  d
0     Monday  24   1  34.0  3
1    Tuesday   4   7   8.0  2
2  Wednesday   3   6   3.0  1
3   Thursday   2   6   4.0  0
4     Friday   1  34 -11.5 -1
5   Saturday   0   2 -21.0 -2
6     Sunday  -1   4 -30.5 -3}

我知道这种格式不正确,因为它与从头开始制作测试 excel 文件时的格式不匹配;列未正确对齐。这也使我什至无法使用以下方法打印列:

print(data.columns)

返回

AttributeError: 'dict' 对象没有属性 'columns'

有没有一种简单的方法来重新格式化数据,以便可以引用和绘制列?

【问题讨论】:

    标签: excel python-3.x pandas data-structures


    【解决方案1】:

    我认为data 是一个数据框字典,每张 Excel 文件都有一个条目;您应该能够使用data['Sheet1'] 访问各个数据帧。

    【讨论】:

      猜你喜欢
      • 2010-10-29
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 2021-06-02
      • 2014-03-26
      • 2015-06-09
      相关资源
      最近更新 更多