一种简单(且通用)的方法是使用您感兴趣的子集创建数据框的视图(或者,根据您的情况,创建一个包含除您要忽略的列之外的所有列的视图),然后使用申请该视图。
In [116]: df
Out[116]:
a b c d f
0 one 3 0.493808 a bob
1 two 8 0.150585 b alice
2 one 6 0.641816 c michael
3 two 5 0.935653 d joe
4 one 1 0.521159 e kate
使用您喜欢的方法来创建您需要的视图。您可以选择一系列列,例如 df_view = df.ix[:,'b':'d'],但以下内容可能对您的方案更有用:
#I want all columns except two
cols = df.columns.tolist()
mycols = [x for x in cols if not x in ['a','f']]
df_view = df[mycols]
将您的函数应用于该视图。 (请注意,这还没有改变 df 中的任何内容。)
In [158]: df_view.apply(lambda x: x /2)
Out[158]:
b c d
0 1 0.246904 20
1 4 0.075293 25
2 3 0.320908 28
3 2 0.467827 28
4 0 0.260579 24
使用 update() 更新 df
In [156]: df.update(df_view.apply(lambda x: x/2))
In [157]: df
Out[157]:
a b c d f
0 one 1 0.246904 20 bob
1 two 4 0.075293 25 alice
2 one 3 0.320908 28 michael
3 two 2 0.467827 28 joe
4 one 0 0.260579 24 kate