Skip to main content
 首页 » 编程设计

python之Pandas之根据列名和行值应用样式/格式

2024年10月01日6lori

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”的任何行的货币 $ 格式

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

谢谢!

请您参考如下方法:

对于第一个条件,如果只有数字的百分比可能会跟随也可以:

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

对于第二个条件,您可以使用,例如如果 METRIC 是 Sales,则除以其他列值,如果 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'] 

并对每一行使用 lambda 和 apply:

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)