向数据框添加新列并设置MultiIndex

时间:2019-07-23 07:42:18

标签: python pandas

我想添加一个新列app_vendor_id并将其设置为带有currency的MultIndex,但是出现错误。

我的代码:

currency = np.array(['BTC','ETH','BCH'])
u = np.array([5000,10000,1046])
cl_bal = pd.DataFrame(np.repeat(u, len(created_at)), index= 
pd.MultiIndex.from_product([currency, created_at], names= 
['currency', 'created_at']), dtype= int)
cl_bal = cl_bal.pivot_table(index='currency', columns= 'created_at')
cl_bal.columns = cl_bal.columns.droplevel(0)
cl_bal['app_vendor_id'] = 3
cl_bal.set_index(['app_vendor_id', 'currency'])

错误:

  

文件   “ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py”,   第3078行,位于get_loc中       返回self._engine.get_loc(key)         在pandas._libs.index.IndexEngine.get_loc中的文件“ pandas / _libs / index.pyx”,第140行          在pandas._libs.index.IndexEngine.get_loc中的文件“ pandas / _libs / index.pyx”,第162行         在pandas._libs.hashtable.PyObjectHashTable.get_item中的文件“ pandas / _libs / hashtable_class_helper.pxi”,行1492          在pandas._libs.hashtable.PyObjectHashTable.get_item中的文件“ pandas / _libs / hashtable_class_helper.pxi”,行1500       KeyError:“货币”

在处理上述异常期间,发生了另一个异常:

  

回溯(最近一次通话最后一次):文件“ histo_var.py”,第202行,   在cl_bal.set_index(['app_vendor_id','currency'])文件中   “ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame

     

第3909行,在set_index中       级别=帧[col] ._ values         文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py”,   第2688行,在 getitem       返回self._getitem_column(key)         文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py”,   _getitem_column中的第2695行       返回self._get_item_cache(key)         文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py”,   第2489行,在_get_item_cache中       值= self._data.get(项目)         文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/internals.py”,   4115行,进入       loc = self.items.get_loc(item)         文件“ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py”,   第3080行,位于get_loc中       返回self._engine.get_loc(self._maybe_cast_indexer(key))         在pandas._libs.index.IndexEngine.get_loc中的文件“ pandas / _libs / index.pyx”,第140行         在pandas._libs.index.IndexEngine.get_loc中的文件“ pandas / _libs / index.pyx”,第162行         在pandas._libs.hashtable.PyObjectHashTable.get_item中的文件“ pandas / _libs / hashtable_class_helper.pxi”,行1492       pandas._libs.hashtable.PyObjectHashTable.get_item       KeyError:“货币”

1 个答案:

答案 0 :(得分:0)

DataFrame.set_index中使用参数append=True,因为currency已经在index中:

created_at = [1,2,3,4]
currency = np.array(['BTC','ETH','BCH'])
u = np.array([5000,10000,1046])
cl_bal = pd.DataFrame(np.repeat(u, len(created_at)), 
                      index=pd.MultiIndex.from_product([currency, created_at], 
                                                       names= ['currency', 'created_at']), 
                                                       dtype= int)
cl_bal = cl_bal.pivot_table(index='currency', columns= 'created_at')
cl_bal.columns = cl_bal.columns.droplevel(0)
cl_bal['app_vendor_id'] = 3
df = cl_bal.set_index(['app_vendor_id'], append=True)
print (df)
created_at                  1      2      3      4
currency app_vendor_id                            
BCH      3               1046   1046   1046   1046
BTC      3               5000   5000   5000   5000
ETH      3              10000  10000  10000  10000