Python Can';在数据帧旋转后不读取列

Python Can';在数据帧旋转后不读取列,python,pandas,pivot,Python,Pandas,Pivot,我有一个表,我来自一个透视表,以消除缺少的值和太短的值,如城市名称,这是我的代码 company = pd.read_sql('SELECT user_id, address FROM company' , con=db_connection) table = pd.pivot_table(company, index=['address'],aggfunc=np.sum) table.reset_index() 然后我得到他的 address

我有一个表,我来自一个透视表,以消除缺少的值和太短的值,如城市名称,这是我的代码

company = pd.read_sql('SELECT user_id, address FROM company' , con=db_connection)
table = pd.pivot_table(company, index=['address'],aggfunc=np.sum)
table.reset_index()
然后我得到他的

    address                                             user_id
3   Jl. Raya Kranggan No. 7, Ruko Kav V No. 1 Jat...    65132
4   #ALAMAT atau LOKASI\r\nKota bengkulu perhubung...   15570
5   '--!>'</script/><Svg/Onload=confirm`alamat bis...   48721
6   (Rumah Bpk.RA'IS) Jl.Puskesmas RT.004/11 No.29...   20786
那我就不能叫专栏了

table['address']
当我称之为专栏时,这种情况就发生了

 KeyError                                  Traceback (most recent call last)
    C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
       2392             try:
    -> 2393                 return self._engine.get_loc(key)
       2394             except KeyError:

    pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)()

    pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)()

    pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)()

    pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)()

KeyError: 'address'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-46-eef3b78ea5fd> in <module>()
----> 1 table['address'] #.astype(str)

C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2060             return self._getitem_multilevel(key)
   2061         else:
-> 2062             return self._getitem_column(key)
   2063 
   2064     def _getitem_column(self, key):

C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2067         # get column
   2068         if self.columns.is_unique:
-> 2069             return self._get_item_cache(key)
   2070 
   2071         # duplicate columns & possible reduce dimensionality

C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   1532         res = cache.get(item)
   1533         if res is None:
-> 1534             values = self._data.get(item)
   1535             res = self._box_item_values(item, values)
   1536             cache[item] = res

C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   3588 
   3589             if not isnull(item):
-> 3590                 loc = self.items.get_loc(item)
   3591             else:
   3592                 indexer = np.arange(len(self.items))[isnull(self.items)]

C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2393                 return self._engine.get_loc(key)
   2394             except KeyError:
-> 2395                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2396 
   2397         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)()

KeyError: 'address'
keyrerror回溯(最近一次调用)
C:\Users\asus\Anaconda3\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2392请尝试:
->2393自动返回发动机。获取位置(钥匙)
2394键错误除外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get\u loc(熊猫\\u libs\index.c:5239)()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get\u loc(熊猫\\u libs\index.c:5085)()
pandas\\ libs\hashtable\类\助手.pxi在pandas.\ libs.hashtable.PyObjectHashTable.get\项中(pandas\\ libs\hashtable.c:20405)()
pandas\\ libs\hashtable\类\助手.pxi在pandas.\ libs.hashtable.PyObjectHashTable.get\项中(pandas\\ libs\hashtable.c:20359)()
KeyError:“地址”
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在()
---->1表['address']#.aType(str)
C:\Users\asus\Anaconda3\lib\site packages\pandas\core\frame.py in\uuuu\getitem\uuuuuu(self,key)
2060返回自我。\u获取项目\u多级(键)
2061其他:
->2062返回自我。\u获取项目\u列(键)
2063
2064 def_getitem_列(自身,键):
C:\Users\asus\Anaconda3\lib\site packages\pandas\core\frame.py(self,key)
2067#获取列
2068如果self.columns.u是唯一的:
->2069返回自。\u获取\u项目\u缓存(密钥)
2070
2071#重复列和可能的降维
C:\Users\asus\Anaconda3\lib\site packages\pandas\core\generic.py in\u get\u item\u缓存(self,item)
1532 res=cache.get(项)
1533如果res为无:
->1534 values=self.\u data.get(项目)
1535 res=自身。_框_项_值(项,值)
1536缓存[项目]=res
get中的C:\Users\asus\Anaconda3\lib\site packages\pandas\core\internals.py(self、item、fastpath)
3588
3589如果不为空(项目):
->3590 loc=自身物品。获取loc(物品)
3591其他:
3592 indexer=np.arange(len(self.items))[isnull(self.items)]
C:\Users\asus\Anaconda3\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2393自动返回发动机。获取位置(钥匙)
2394键错误除外:
->2395返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2396
2397 indexer=self.get\u indexer([key],method=method,tolerance=tolerance)
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get\u loc(熊猫\\u libs\index.c:5239)()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get\u loc(熊猫\\u libs\index.c:5085)()
pandas\\ libs\hashtable\类\助手.pxi在pandas.\ libs.hashtable.PyObjectHashTable.get\项中(pandas\\ libs\hashtable.c:20405)()
pandas\\ libs\hashtable\类\助手.pxi在pandas.\ libs.hashtable.PyObjectHashTable.get\项中(pandas\\ libs\hashtable.c:20359)()
KeyError:“地址”

如果您有其他解决方案,只要我可以映射要执行的地址就可以了,因为
地址是索引名,没有列:

table = pd.pivot_table(company, index='address',aggfunc=np.sum).reset_index()
如果要为聚合定义列
sum
,另一种解决方案是:

table = company.groupby('address', as_index=False)['user_id'].sum()
或:

对于所有列:

table = company.groupby('address', as_index=False).sum()


我想您需要一个back的Sign输出,因为
address
是索引名,没有列:

table = pd.pivot_table(company, index='address',aggfunc=np.sum).reset_index()
如果要为聚合定义列
sum
,另一种解决方案是:

table = company.groupby('address', as_index=False)['user_id'].sum()
或:

对于所有列:

table = company.groupby('address', as_index=False).sum()


我认为
pivot
在这里不是一个合适的选择

您可以使用以下选项:

company.groupby('address').sum()

我认为
pivot
在这里不是一个合适的选择

您可以使用以下选项:

company.groupby('address').sum()

table[table.columns[1]
给出了什么
indexer错误:索引1超出了轴0的范围,大小为1
请重新检查
table.columns
如果有两个元素,那么索引1应该工作
address
是一个索引名称-而不是一列可能您忘记了分配回
table=table.reset\u index()
table[table.columns[1]]
给出了什么
索引错误:索引1超出了轴0的范围,大小为1
请重新检查
table.columns
如果有两个元素,则索引1应该起作用
address
是一个索引名称-而不是一列可能您忘记了分配回
table=table.reset\u index()
我还需要重置吗index@NabihIbrahimBawazir,您可以使用
作为_index=False
参数,而不是我仍然需要重置吗index@NabihIbrahimBawazir,您可以改为使用
作为_index=False
参数