python3.5 - python使用pandas的交叉表crosstab出现问题
天蓬老师
天蓬老师 2017-04-18 09:04:16
0
1
885

对数据分析时使用到pandas,下面的代码是从数据库中获取数据再转换成DataFrame结构

sql = 'select * from content;'
cur.execute(sql)
rows = cur.fetchall()
datas = []
for data in rows:
    datas.append(data)
frame = DataFrame(datas)

数据库的表结构如下:

下面就是数据操作的代码:

def get_education():
    print(frame)
    print(frame[10])
    print(frame[8])
    print(type(frame[10]))
    city_education = pd.crosstab(frame[10], frame[8], margins=True).sort_values(by='All', ascending=False)[:11]
    print(city_education)

然后就一直出现错误:

<class 'pandas.core.series.Series'>
Traceback (most recent call last):
  File "D:/python爬虫/python爬虫/爬取拉钩.py", line 301, in <module>
    get_education()
  File "D:/python爬虫/python爬虫/爬取拉钩.py", line 249, in get_education
    city_education = pd.crosstab(frame[10], frame[8], margins=True).sort_values(by='All', ascending=False)[:11]
  File "F:\Python35\lib\site-packages\pandas\tools\pivot.py", line 462, in crosstab
    aggfunc=len, margins=margins, dropna=dropna)
  File "F:\Python35\lib\site-packages\pandas\tools\pivot.py", line 158, in pivot_table
    margins_name=margins_name)
  File "F:\Python35\lib\site-packages\pandas\tools\pivot.py", line 180, in _add_margins
    if margins_name in table.index.get_level_values(level):
  File "F:\Python35\lib\site-packages\pandas\indexes\base.py", line 2025, in get_level_values
    self._validate_index_level(level)
  File "F:\Python35\lib\site-packages\pandas\indexes\base.py", line 1163, in _validate_index_level
    (level + 1))
IndexError: Too many levels: Index has only 1 level, not 11

求解答,刚接触pandas,但也参考过别人的,他也是这样写的。
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
刘奇

直接用pandas就可以读取

import pandas as pd

pd.read_sql("select * from content", con)# con相当于你上面的cur,然后,注意,sql末尾不要加分号
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板