suchen

Heim  >  Fragen und Antworten  >  Hauptteil

python3.5 - python使用pandas的交叉表crosstab出现问题

对数据分析时使用到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,但也参考过别人的,他也是这样写的。
天蓬老师天蓬老师2804 Tage vor902

Antworte allen(1)Ich werde antworten

  • 怪我咯

    怪我咯2017-04-18 09:06:16

    直接用pandas就可以读取

    import pandas as pd
    
    pd.read_sql("select * from content", con)# con相当于你上面的cur,然后,注意,sql末尾不要加分号

    Antwort
    0
  • StornierenAntwort