索引复合时,pandas多列应用失败

时间:2019-03-15 15:02:53

标签: python pandas

我想在熊猫数据框的另一列上使用一列来apply

# 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]

当我像这样操作apply时:

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

我真的不明白为什么会这样。

有什么解决方案可以解决此问题?

编辑: 我希望结果数据框像这样:

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

2 个答案:

答案 0 :(得分:4)

这似乎是您的索引是可变对象的问题。如果先重置,一切就开始起作用。

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]

答案 1 :(得分:2)

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

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