假设我有一个MultiIndex系列s
:
>>> s
values
a b
1 2 0.1
3 6 0.3
4 4 0.7
我想应用一个使用行索引的函数:
def f(x):
# conditions or computations using the indexes
if x.index[0] and ...:
other = sum(x.index) + ...
return something
我如何为这样的功能做s.apply(f)
?这种操作的推荐方法是什么?我期望获得一个新系列,其中每个行应用此函数产生的值和相同的MultiIndex。
答案 0 :(得分:35)
我不相信apply
可以访问索引;它将每一行视为一个numpy对象,而不是一个系列,如你所见:
In [27]: s.apply(lambda x: type(x))
Out[27]:
a b
1 2 <type 'numpy.float64'>
3 6 <type 'numpy.float64'>
4 4 <type 'numpy.float64'>
要解决此限制,请将索引提升为列,应用您的函数,然后使用原始索引重新创建系列。
Series(s.reset_index().apply(f, axis=1).values, index=s.index)
其他方法可能会使用s.get_level_values
,这在我看来常常有点难看,或s.iterrows()
,这可能会更慢 - 可能取决于f
的具体内容。
答案 1 :(得分:11)
使它成为一个框架,如果你想要返回标量(所以结果是一个系列)
设置
In [11]: s = Series([1,2,3],dtype='float64',index=['a','b','c'])
In [12]: s
Out[12]:
a 1
b 2
c 3
dtype: float64
打印功能
In [13]: def f(x):
print type(x), x
return x
....:
In [14]: pd.DataFrame(s).apply(f)
<class 'pandas.core.series.Series'> a 1
b 2
c 3
Name: 0, dtype: float64
<class 'pandas.core.series.Series'> a 1
b 2
c 3
Name: 0, dtype: float64
Out[14]:
0
a 1
b 2
c 3
由于您可以在此返回任何内容,只需返回标量(通过name
属性访问索引)
In [15]: pd.DataFrame(s).apply(lambda x: 5 if x.name == 'a' else x[0] ,1)
Out[15]:
a 5
b 2
c 3
dtype: float64
答案 2 :(得分:5)
转换为DataFrame
并沿行应用。您可以访问索引x.name
。 x
也是Series
现在有1个值
s.to_frame(0).apply(f, axis=1)[0]
答案 3 :(得分:1)
可以更快地找到where
而不是apply
:
In [11]: s = pd.Series([1., 2., 3.], index=['a' ,'b', 'c'])
In [12]: s.where(s.index != 'a', 5)
Out[12]:
a 5
b 2
c 3
dtype: float64
您还可以将numpy风格的逻辑/函数用于任何部分:
In [13]: (2 * s + 1).where((s.index == 'b') | (s.index == 'c'), -s)
Out[13]:
a -1
b 5
c 7
dtype: float64
In [14]: (2 * s + 1).where(s.index != 'a', -s)
Out[14]:
a -1
b 5
c 7
dtype: float64
我建议测试速度(因为效率取决于功能)。虽然,我发现apply
更具可读性......
答案 4 :(得分:0)
如果使用DataFrame.apply()而不是Series.apply(),则可以在fucntion中作为参数访问整行。
def f1(row):
if row['I'] < 0.5:
return 0
else:
return 1
def f2(row):
if row['N1']==1:
return 0
else:
return 1
import pandas as pd
import numpy as np
df4 = pd.DataFrame(np.random.rand(6,1), columns=list('I'))
df4['N1']=df4.apply(f1, axis=1)
df4['N2']=df4.apply(f2, axis=1)
答案 5 :(得分:0)
使用reset_index()
将Series转换为DataFrame,将索引转换为列,然后将apply
的函数转换为DataFrame。
棘手的部分是知道reset_index()
如何命名列,因此这里有一些示例。
s=pd.Series({'idx1': 'val1', 'idx2': 'val2'})
def use_index_and_value(row):
return 'I made this with index {} and value {}'.format(row['index'], row[0])
s2 = s.reset_index().apply(use_index_and_value, axis=1)
# The new Series has an auto-index;
# You'll want to replace that with the index from the original Series
s2.index = s.index
s2
输出:
idx1 I made this with index idx1 and value val1
idx2 I made this with index idx2 and value val2
dtype: object
这里的概念相同,但是您需要以row['level_*']
的形式访问索引值,因为这是Series.reset_index()
放置索引值的地方。
s=pd.Series({
('idx(0,0)', 'idx(0,1)'): 'val1',
('idx(1,0)', 'idx(1,1)'): 'val2'
})
def use_index_and_value(row):
return 'made with index: {},{} & value: {}'.format(
row['level_0'],
row['level_1'],
row[0]
)
s2 = s.reset_index().apply(use_index_and_value, axis=1)
# Replace auto index with the index from the original Series
s2.index = s.index
s2
输出:
idx(0,0) idx(0,1) made with index: idx(0,0),idx(0,1) & value: val1
idx(1,0) idx(1,1) made with index: idx(1,0),idx(1,1) & value: val2
dtype: object
如果系列或索引有名称,则需要进行相应的调整。