簡體   English   中英

大數據框的Pandas IndexError

[英]Pandas IndexError for large dataframe

當我嘗試向大型DataFrame添加新列時,出現IndexError。 有誰能幫助我解決這個錯誤?

>vec
                 0        1        2        3        4        5        6 
V1.UC8.0         0        0        0        0        0        0        0   
V1.UC48.0        0        0        0        0        0        0        0   

                 7        8        9         ...     2546531  2546532  2546533  
V1.UC8.0         0        0        0   ...           0        0        0   
V1.UC48.0        0        0        0   ...           0        0        0   

               2546534  2546535  2546536  2546537  2546538  2546539  2546540  
V1.UC8.0         0        0        0        0        0        0        0  
V1.UC48.0        0        0        0        0        0        0        0  

[2 rows x 2546541 columns]

> vec['ToDrop']=0


    IndexError                                Traceback (most recent call last)
<ipython-input-40-9868611037ed> in <module>()
----> 1 vec['ToDrop']=0

C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in __setitem__(self, key, value)
   2115         else:
   2116             # set column
-> 2117             self._set_item(key, value)
   2118 
   2119     def _setitem_slice(self, key, value):

C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in _set_item(self, key, value)
   2193         self._ensure_valid_index(value)
   2194         value = self._sanitize_column(key, value)
-> 2195         NDFrame._set_item(self, key, value)
   2196 
   2197         # check if we are modifying a copy

C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in _set_item(self, key, value)
   1188 
   1189     def _set_item(self, key, value):
-> 1190         self._data.set(key, value)
   1191         self._clear_item_cache()
   1192 

C:\Anaconda\lib\site-packages\pandas\core\internals.pyc in set(self, item, value, check)
   2970 
   2971         try:
-> 2972             loc = self.items.get_loc(item)
   2973         except KeyError:
   2974             # This item wasn't present, just insert at end

C:\Anaconda\lib\site-packages\pandas\core\index.pyc in get_loc(self, key, method)
   1435         """
   1436         if method is None:
-> 1437             return self._engine.get_loc(_values_from_object(key))
   1438 
   1439         indexer = self.get_indexer([key], method=method)

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

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

pandas\src\util.pxd in util.get_value_at (pandas\index.c:15287)()

IndexError: index out of bounds

我一直在嘗試向轉置的DataFrame(vec.T)添加新行,但是遇到了相同的錯誤。

確實這很奇怪。

您可以使用以下方法作為解決方法:

vec = pd.merge(vec, pd.DataFrame([0, 0], columns=["new"]), right_index=True, left_index=True)  # Optional: pass copy=False

確保新的1列數據框具有與vec相同的索引。

有關為什么這很奇怪的更多信息:

希望有人可以提供適當的答案。

df = pd.DataFrame(np.zeros((2, 2546540)))
df[2546540] = 0

輸出: IndexError如OP的文章中所述。

df["blah"] = 0

輸出:

TypeError: unorderable types: numpy.ndarray() < str()

同時,使用小型數據框就可以了:

df = pd.DataFrame(np.zeros((2, 200)))
df[200] = 0

輸出完全符合預期:

   0    1    2    3    4    5    6    7    8    9   ...   191  192  193  194  0    0    0    0    0    0    0    0    0    0    0 ...     0    0    0    0   
1    0    0    0    0    0    0    0    0    0    0 ...     0    0    0    0   

   195  196  197  198  199  200  
0    0    0    0    0    0    0  
1    0    0    0    0    0    0  

[2 rows x 201 columns]

希望這會有所幫助,有人可以解釋這種熊貓行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM