对数据分析时使用到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)
然后就一直出现错误:
Traceback (most recent call last):
File "D:/python爬虫/python爬虫/爬取拉钩.py", line 301, in
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,但也参考过别人的,他也是这样写的。