Python 属性错误:';系列';对象没有属性';到sql';

Python 属性错误:';系列';对象没有属性';到sql';,python,python-2.7,pandas,sqlite,Python,Python 2.7,Pandas,Sqlite,我正在将查询导入pandas数据框,然后创建一个名为hindex的结果数据框,以导入到我的数据库表中,如下所示: import sqlite3 import numpy as np import pandas as pd #access the database created db = sqlite3.connect('test/publications') df = pd.read_sql("select AuthorID as ID, citations from publicatio

我正在将查询导入pandas数据框,然后创建一个名为
hindex
的结果数据框,以导入到我的数据库表中,如下所示:

import sqlite3
import numpy as np
import pandas as pd

#access the database created
db = sqlite3.connect('test/publications')

df = pd.read_sql("select AuthorID as ID, citations from publications as p join authors_publications as a on p.ID=a.PaperID order by AuthorID, citations desc", db)

df2 = df.sort(['ID','Citations'],ascending=['Citations','ID'])
groups = df2.groupby('ID')
ind2 = np.array([np.arange(len(g))+1 for g in groups.groups.itervalues()])
df2['newindex'] = np.hstack(ind2)
df2['condition'] = df2['Citations']>=df2['newindex']
hindex = df2.groupby('ID').sum()['condition']

hindex.to_sql('authors_hindex', db, flavor='sqlite', if_exists='replace', index=True)
我以前使用过sql,它很管用。不知道为什么不在这里。我得到以下错误:

AttributeError                     Traceback (most recent call last)
<ipython-input-4-0748af5dad1d> in <module>()
     43 
     44 print hindex
---> 45 hindex.to_sql('authors_hindex', db, flavor='sqlite', if_exists='replace', index=True)
     46 

/usr/lib/python2.7/dist-packages/pandas/core/generic.pyc in __getattr__(self, name)
   1813                 return self[name]
   1814             raise AttributeError("'%s' object has no attribute '%s'" %
-> 1815                                  (type(self).__name__, name))
   1816 
   1817     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'to_sql'
AttributeError回溯(最近一次调用)
在()
43
44印地语
--->45 hindex.to_sql('authors_hindex',db,flavor='sqlite',如果_exists='replace',index=True)
46
/usr/lib/python2.7/dist-packages/pandas/core/generic.pyc in____getattr___(self,name)
1813返回自我[姓名]
1814 raise AttributeError(“%s”对象没有属性“%s”
->1815(类型(自身)。_名称_;名称))
1816
1817定义设置属性(自身、名称、值):
AttributeError:“Series”对象没有“to_sql”属性
试试这个:

hindex = df2.groupby('ID').sum()[['condition']]
因此,使用双精度
[[]]
将返回df


您的原始行:
hindex=df2.groupby('ID').sum()['condition']
返回了一个系列,但它确实有一个方法,尽管不清楚为什么失败。

您有什么版本的熊猫,因为它甚至可以与Series@EdChum我有0.13.1这是一个旧版本,我不记得它是什么时候添加到系列中的,如果您可以升级,最新的稳定版本是0.16。0@EdChum. 我现在就去。这也能解释为什么我指定index=True时它不将index添加为列吗?to_sql的
方法只在0.14中添加到了Series中。