Pandas Dataframe元组索引isin:缓冲区的维数错误(预期1,得2)

时间:2018-03-28 15:32:28

标签: python pandas graph

我有一个使用元组作为索引的数据框(图形的边),我想用一组元组(第二个图形的边)过滤第一个数据帧。我正在尝试使用' isin'通过第二个图形中的一组元组过滤第一个图形的索引的方法:

import pandas as pd

index1 = ['a', 'b', 'c', 'd', 'e']
index2 = [1, 2, 3, 4, 5]
graph1_edges = list(zip(index1, index2))

attributes= ['apple', 'banana', 'pumkin', 'spice', 'sugar']

df_graph1 = pd.DataFrame(attributes, index=graph1_edges, columns=['Ingredients'])

graph2_edges = set([('a',1), ('c',3), ('e', 5), ('t',10)])

# Problem Code
df_graph1[df_graph1.index.isin(graph2_edges)]

我希望只返回df_graph1(' a',1),(' c',3),(' e',5)索引行。相反,我得到了这个:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-194-116c5e1e0cec> in <module>()
      9 shopping_key = set([('a',1), ('c',3), ('e', 5), ('t',10)])
     10 
---> 11 df.index.isin(shopping_key)

C:\Users\joshu\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in isin(self, values, level)
   2766         if level is not None:
   2767             self._validate_index_level(level)
-> 2768         return algos.isin(np.array(self), values)
   2769 
   2770     def _can_reindex(self, indexer):

C:\Users\joshu\Anaconda3\lib\site-packages\pandas\core\algorithms.py in isin(comps, values)
    419             comps = comps.astype(object)
    420 
--> 421     return f(comps, values)
    422 
    423 

C:\Users\joshu\Anaconda3\lib\site-packages\pandas\core\algorithms.py in <lambda>(x, y)
    397     # work-around for numpy < 1.8 and comparisions on py3
    398     # faster for larger cases to use np.in1d
--> 399     f = lambda x, y: htable.ismember_object(x, values)
    400     if (_np_version_under1p8 and compat.PY3) or len(comps) > 1000000:
    401         f = lambda x, y: np.in1d(x, y)

pandas\_libs\hashtable_func_helper.pxi in pandas._libs.hashtable.ismember_object (pandas\_libs\hashtable.c:29677)()

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

我似乎无法理清错误的含义。有什么建议吗?

0 个答案:

没有答案