Pandas - 根据列名和行的值应用样式/格式

时间:2017-09-24 22:10:50

标签: python pandas dataframe

import pandas as pd

df2 = pd.DataFrame.from_records(
{'Compared': {0: -0.090909090999999997,
  1: -0.130434783,
  2: -0.10714285699999999,
  3: -0.15966386599999999},
 'LastYR': {0: 5500.0, 1: 115.0, 2: 84.0, 3: 40520.523399999998},
 'METRIC': {0: 'Visits', 1: 'Units', 2: 'Orders', 3: 'Sales'},
 'Today': {0: 5000.0, 1: 100.0, 2: 75.0, 3: 34050.860000000001},
 'region_rollup': {0: 'America', 1: 'America', 2: 'America', 3: 'America'}}

)
df2.head()

我如何申请:

a)我想要的任何整个列的%格式

b)货币$格式到METRIC值为'Sales'的任何行

enter image description here

我已经尝试过df.style.format()函数,这将让我对列进行子集化并应用%格式,但我无法确定要编写哪种函数,这样我就可以根据另一列的价值(再次,“如果指标=销售然后今天格式化为货币”,或多或少)。

谢谢!

1 个答案:

答案 0 :(得分:2)

对于第一个条件,如果仅跟随数字的百分比也可以起作用:

df2['%'] = (df2['LastYR']/ df2['LastYR'].sum()) * 100

对于第二个条件,可能是您可以使用,例如如果METRICSales,则将下面的其他列值除以Sales,然后将Today列除以100.0,否则保持与Today列值相同:

df2['Currency'] = df2.apply(lambda row: (row['Today'] / 100.0 if row['METRIC'] == 'Sales' else row['Today']), axis=1)

结果:

   Compared      LastYR  METRIC     Today region_rollup          %   Currency
0 -0.090909   5500.0000  Visits   5000.00       America  11.899733  5000.0000
1 -0.130435    115.0000   Units    100.00       America   0.248813   100.0000
2 -0.107143     84.0000  Orders     75.00       America   0.181741    75.0000
3 -0.159664  40520.5234   Sales  34050.86       America  87.669713   340.5086

更新

使用功能并使用apply

def test_function(row):
    if row['METRIC'] == 'Sales':
        return row['Today'] / 100.0 
    else:
        return row['Today']

每行使用lambdaapply

df2['Currency'] = df2.apply(lambda row: test_function(row), axis=1)

更新2:

def test_function(row, column):
    if row['METRIC'] == 'Sales':
        return row[column] / 100.0 
    else:
        return row[column]

df2['%'] = (df2['LastYR']/ df2['LastYR'].sum()) * 100

column_list = ['Today', 'LastYR']
for column in column_list:
    df2[column] = df2.apply(lambda row: test_function(row, column), axis=1)