将函数应用于依赖于pandas中列名称的DataFrame中的每个单元格 [英] Apply function to each cell in DataFrame that depends on the column name in pandas
问题描述
如何将函数应用于取决于列名称的DataFrame中的每个单元格?
How can I apply function to each cell in a DataFrame that depends on the column name?
我知道 pandas.DataFrame. applymap ,但根据列名似乎不允许:
I'm aware of pandas.DataFrame.applymap but it doesn't seem to allow depending on the column name:
import numpy as np
import pandas as pd
np.random.seed(1)
frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'),
index=['Utah', 'Ohio', 'Texas', 'Oregon'])
print(frame)
format = lambda x: '%.2f' % x
frame = frame.applymap(format)
print(frame)
返回:
b d e
Utah 1.624345 -0.611756 -0.528172
Ohio -1.072969 0.865408 -2.301539
Texas 1.744812 -0.761207 0.319039
Oregon -0.249370 1.462108 -2.060141
b d e
Utah 1.62 -0.61 -0.53
Ohio -1.07 0.87 -2.30
Texas 1.74 -0.76 0.32
Oregon -0.25 1.46 -2.06
相反,我希望应用到每个单元格的函数将当前单元格的列名用作参数.
Instead, I want the function that I applied to each cell to use the column name of the current cell as an argument.
我不想遍历每一列,例如:
I don't want to have to loop myself over each column, like:
def format2(cell_value, column_name):
return '{0}_{1:.2f}'.format(column_name, cell_value)
for column_name in frame.columns.values:
print('column_name: {0}'.format(column_name))
frame[column_name]=frame[column_name].apply(format2, args=(column_name))
print(frame)
返回:
b d e
Utah b_1.62 d_-0.61 e_-0.53
Ohio b_-1.07 d_0.87 e_-2.30
Texas b_1.74 d_-0.76 e_0.32
Oregon b_-0.25 d_1.46 e_-2.06
(这只是一个例子.我想在单元格上应用的功能可能不只是添加列名而已)
(This is just one example. The functions I want to apply on the cells may do more than just appending the column name)
推荐答案
我改进了另一个答案,默认情况下axis=0
是因此可以省略:
I bit improved another answer, axis=0
is by default so can be omit:
a = frame.apply(lambda x: x.apply(format2,args=(x.name)))
print (a)
b d e
Utah b_1.62 d_-0.61 e_-0.53
Ohio b_-1.07 d_0.87 e_-2.30
Texas b_1.74 d_-0.76 e_0.32
Oregon b_-0.25 d_1.46 e_-2.06
这篇关于将函数应用于依赖于pandas中列名称的DataFrame中的每个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!