Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当索引为复合索引时,多列应用失败_Python_Pandas - Fatal编程技术网

Python 当索引为复合索引时,多列应用失败

Python 当索引为复合索引时,多列应用失败,python,pandas,Python,Pandas,我想应用一个函数,在数据帧中的另一列上使用一列 # function to select an item from a list in a column def select_index(r, i): return list(np.take(r, i)) # create DataFrame col = ['index', 'Column2', 'Column3'] d = {'index': [['a1', 'a2', 'a3'], ['a10', 'a20', 'a30']],

我想
应用
一个函数,在数据帧中的另一列上使用一列

# function to select an item from a list in a column
def select_index(r, i):
    return list(np.take(r, i))

# create DataFrame
col = ['index', 'Column2', 'Column3']

d = {'index': [['a1', 'a2', 'a3'], ['a10', 'a20', 'a30']],
     'Column2': [['b1', 'b2', 'b3'], ['b10', 'b20', 'b30']],
     'Column3': [[0, 1], [1, 2]]
    }

df = pd.DataFrame(data=d, columns=col)
df.set_index('index', inplace=True)

print(df)

                         Column2 Column3
index                                   
[a1, a2, a3]        [b1, b2, b3]  [0, 1]
[a10, a20, a30]  [b10, b20, b30]  [1, 2]
当我执行
应用
时,如下所示:

df['Column2'] = df[['Column2', 'Column3']].apply(lambda x: select_index(*x), axis=1)
我得到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-241-60f4b4130a38> in <module>
      1 df.loc = df[['Column2', 'Column3']].apply(
      2     lambda x: select_index(*x),
----> 3     axis=1
      4 )

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6012                          args=args,
   6013                          kwds=kwds)
-> 6014         return op.get_result()
   6015 
   6016     def applymap(self, func):

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/apply.py in get_result(self)
    140             return self.apply_raw()
    141 
--> 142         return self.apply_standard()
    143 
    144     def apply_empty_result(self):

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/apply.py in apply_standard(self)
    246 
    247         # compute the result using the series generator
--> 248         self.apply_series_generator()
    249 
    250         # wrap results

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/apply.py in apply_series_generator(self)
    274         else:
    275             try:
--> 276                 for i, v in enumerate(series_gen):
    277                     results[i] = self.f(v)
    278                     keys.append(v.name)

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/apply.py in <genexpr>(.0)
    365         constructor = self.obj._constructor_sliced
    366         return (constructor(arr, index=self.columns, name=name)
--> 367                 for i, (arr, name) in enumerate(zip(self.values,
    368                                                     self.index)))
    369 

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    279         generic.NDFrame.__init__(self, data, fastpath=True)
    280 
--> 281         self.name = name
    282         self._set_axis(0, index, fastpath=True)
    283 

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/generic.py in __setattr__(self, name, value)
   4396             object.__setattr__(self, name, value)
   4397         elif name in self._metadata:
-> 4398             object.__setattr__(self, name, value)
   4399         else:
   4400             try:

~/miniconda3/envs/cl/lib/python3.6/site-packages/pandas/core/series.py in name(self, value)
    406     def name(self, value):
    407         if value is not None and not is_hashable(value):
--> 408             raise TypeError('Series.name must be a hashable type')
    409         object.__setattr__(self, '_name', value)
    410 

TypeError: Series.name must be a hashable type

为什么不在这里使用for循环呢

[select_index (x, y )for x,y in zip(df['Column2'], df['Column3'])]
Out[314]: [['b1', 'b2'], ['b20', 'b30']]

这似乎是索引是可变对象的问题。如果你先重置它,事情就会开始工作

df.reset_index().apply(lambda x: select_index(x['Column2'], x['Column3']), axis=1)

0      [b1, b2]
1    [b20, b30]
dtype: object
或者



我也不明白,因为你还没有包含函数。我的坏更新为什么不?因为这是熊猫和python。@Scotty1-我觉得这是一个很好的用例。@Scotty1-另请参见
apply
@coldspeed的适用性谢谢链接。实际上,对于给定的数据集来说,这似乎是一种很好的方法。不,我最后的评论是想说,我同意for循环在这种情况下可能是一种很好的方法。:)我想保住我的饭碗index@armundle除非
select\u index
正在使用它,否则您实际上不需要这样做。此外,
reset\u index
只会在操作中删除它,而不会永久删除。我将问题更新为我希望生成的数据帧的样子。
df.reset_index().apply(lambda x: select_index(x['Column2'], x['Column3']), axis=1)

0      [b1, b2]
1    [b20, b30]
dtype: object
df.reset_index()[['Column2', 'Column3']].apply(lambda x: select_index(*x), axis=1)

0      [b1, b2]
1    [b20, b30]
dtype: object
df['Column4'] = df.reset_index()[['Column2', 'Column3']].apply(
       lambda x: select_index(*x), axis=1).values
df

                         Column2 Column3     Column4
index                                               
[a1, a2, a3]        [b1, b2, b3]  [0, 1]    [b1, b2]
[a10, a20, a30]  [b10, b20, b30]  [1, 2]  [b20, b30]