python - 向 Dataframe 添加新列并设置 MultiIndex

标签 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'])

错误:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc return self._engine.get_loc(key) File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'currency'

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last): File "histo_var.py", line 202, in cl_bal.set_index(['app_vendor_id', 'currency']) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame

line 3909, in set_index level = frame[col]._values File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 2688, in getitem return self._getitem_column(key) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 2695, in _getitem_column return self._get_item_cache(key) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache values = self._data.get(item) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/internals.py", line 4115, in get loc = self.items.get_loc(item) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'currency'

最佳答案

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

关于python - 向 Dataframe 添加新列并设置 MultiIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159092/

相关文章:

python - 如何在 Python 中合并多个具有不同表列的 Excel 工作表?

python - 将一列值的总和除以数据框中所有行的计数

python - Blaza 和 Pandas 的大数据

python - 生成一个字符串后跟多个 None 值的列表

python - 如何重新加载 python 子模块?

python - 使用 pandas.read_csv 分隔列

python - Pandas - 一个单元格中多个值的 value_counts

python - 连续 pdf 的 KL 散度

javascript - 我们可以在 PUBNUB 中重新发布消息吗

python - 如何让 cProfile 只打印重要的功能?