inplace apply to columns of pandas dataframe satisfying conditions

橙三吉。 提交于 2020-01-03 17:20:31

问题


Consider the following pandas dataframe:

df = pd.DataFrame({'t': [1,2,3], 'x1': [4,5,6], 'x2': [7,8,9]} )

>>> print(df)
t  x1  x2
0  1   4   7
1  2   5   8
2  3   6   9

I would like to apply a function (say multiplying by 2) to those columns with names containing the character 'x'

This can be done by:

df.filter(regex='x').apply(lambda c: 2*c)

but not in place. My solution is:

tmp = df.filter(regex='x')
tmp = tmp.apply(lambda c: 2*c)
tmp['t'] = df['t']
df = tmp

which has the added problem of changing the order of the columns. Is there a better way?


回答1:


IIUC you can do something like this:

In [239]: df.apply(lambda x: x*2 if 'x' in x.name else x)
Out[239]:
   t  x1  x2
0  1   8  14
1  2  10  16
2  3  12  18

UPDATE:

In [258]: df.apply(lambda x: x*2 if 'x' in x.name else x) \
            .rename(columns=lambda x: 'ytext_{}_moretext'.format(x[-1]) if 'x' in x else x)
Out[258]:
   t  ytext_1_moretext  ytext_2_moretext
0  1                 8                14
1  2                10                16
2  3                12                18



回答2:


Use df.columns.str.contains('x') to get boolean mask to slice df

df.loc[:, df.columns.str.contains('x')] *= 2
print(df)

   t  x1  x2
0  1   8  14
1  2  10  16
2  3  12  18

More generalized

def f(x):
    return 2 * x

m = df.columns.str.contains('x')
df.loc[:, m] = f(df.loc[:, m])
print(df)

   t  x1  x2
0  1   8  14
1  2  10  16
2  3  12  18

Using apply

m = df.columns.str.contains('x')
df.loc[:, m] = df.loc[:, m].apply(f)
print(df)

   t  x1  x2
0  1   8  14
1  2  10  16
2  3  12  18


来源:https://stackoverflow.com/questions/43402663/inplace-apply-to-columns-of-pandas-dataframe-satisfying-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!