python - Pandas:根据多个条件生成句子并将其显示在单独的列中

标签 python pandas

这是问题 of this 的后续问题 我有一个数据框如下:

           KPI              Tata      JSW    scope   BIC    Peer   BIC_diff  Avg_diff
0        Gross Margin %    0.5820    0.4760   Max    0.582  0.268        0    0.313 
2          SG&A/Revenue    0.1410    0.0300   Min    0.029  0.0645     0.112  0.0765
3                   ROA    0.0640    0.0930   Max    0.093  0.0457    -0.029  0.0183
4                   ROE    0.1380    0.2430   Max    0.243  0.1024    -0.105  0.0356
5    Inventory Turnover    2.2000    3.2700   Min    1.71   3.892      0.49  -1.692
6        Current Ratio     0.9000    0.8000   Min    0.5    1.15        0.4  -0.25

现在我想添加另一列,其单元格值以 df['scope']df['BIC_diff']df[ 为条件'Peer_diff']。所以结果列如下所示。基本条件如下:

cond_comments = [(df['scope']=='Max') & (df['BIC_diff'] > 0) & (df['Avg_diff'] > 0),
             (df['scope']=='Max') & (df['BIC_diff'] <= 0) & (df['Avg_diff'] <= 0),
             (df['scope']=='Max') & (df['BIC_diff'] <= 0) & (df['Avg_diff'] > 0),
             (df['scope']=='Min') & (df['BIC_diff'] > 0) & (df['Avg_diff'] > 0),
             (df['scope']=='Min') & (df['BIC_diff'] <= 0) & (df['Avg_diff'] <= 0),
             (df['scope']=='Min') & (df['BIC_diff'] > 0) & (df['Avg_diff'] <= 0)]

根据上述条件,我尝试添加另一个名为 comments 的列,如下所示。想法是将注释内相关位置的 KPI 列文本与 BIC_diffPeer_diff 中的值连接 .

       KPI              BIC   Peer   BIC_diff  Avg_diff  comments
   Gross Margin %      0.582  0.268        0    0.313    Gross Margin is better than peer by 31.3% ## <-- `Gross Margin is from KPI. 31.3% is from Avg_diff.
     SG&A/Revenue      0.029  0.0645     0.112  0.0765   There is a scope of improvement for SG&A/Revenue by at least 7.65% ## <-- SG&A is taken from KPI. 7.65% is taken from Avg_diff.
              ROA      0.093  0.0457    -0.029  0.0183   There is a scope of improvement for ROA by 2.90% ## <-- ROA is from KPI. 2.90% is taken from BIC_diff absolute value. 
              ROE      0.243  0.1024    -0.105  0.0356   There is a scope of improvement for ROE by 10.50%
Inventory Turnover     1.71   3.892      0.49  -1.692
    Current Ratio      0.5    1.15        0.4  -0.25

为了实现上述目标,我尝试了以下方法:

cond_comments = [(df['scope']=='Max') & (df['BIC_diff'] > 0) & (df['Avg_diff'] > 0),
             (df['scope']=='Max') & (df['BIC_diff'] <= 0) & (df['Avg_diff'] <= 0),
             (df['scope']=='Max') & (df['BIC_diff'] <= 0) & (df['Avg_diff'] > 0),
             (df['scope']=='Min') & (df['BIC_diff'] > 0) & (df['Avg_diff'] > 0),
             (df['scope']=='Min') & (df['BIC_diff'] <= 0) & (df['Avg_diff'] <= 0),
             (df['scope']=='Min') & (df['BIC_diff'] > 0) & (df['Avg_diff'] <= 0)]
vals_comments = ['{0} is better than BIC and peer by {1} and {2} respectively'.format(df['KPI'],df['BIC_diff'],df['Avg_diff']),
             '{0} has scope of improvement by atleast {1}'.format(df['KPI'],df['Avg_diff']),
             'While {0} is better than its peer, still there is a scope of improvement by {1}'.format(df['KPI'],df['BIC_diff']),
             '{0} has scope of improvement by atleast {1}'.format(df['KPI'],df['Avg_diff']),
             '{0} is better than BIC and peer by {1} and {2} respectively'.format(df['KPI'],df['BIC_diff'],df['Avg_diff']),
             'While {0} is better than its peer, still there is a scope of improvement by {1}'.format(df['KPI'],df['BIC_diff'])]
df['Comments'] = pd.np.select(cond_comments, vals_comments,default='No Comment')

但是,上面的代码没有生成我上面列出的注释。

感谢任何帮助。

PS:请原谅任何可能的格式错误。

最佳答案

我将创建一个函数,首先执行所有条件,然后按行应用它。这样就可以更轻松地添加新条件并查看什么条件会导致什么结果。

def create_comment(line: dict) -> str:
    # column values are accessible as in a dictionary
    if (line['scope']=='Max') and (line['BIC_diff'] > 0) and (line['Avg_diff'] > 0):
        return '{0} is better than BIC and peer by {1} and {2} respectively'.format(line['KPI'],line['BIC_diff'],line['Avg_diff'])
    elif (line['scope']=='Max') and (line['BIC_diff'] <= 0) and (line['Avg_diff'] <= 0):
        return '{0} has scope of improvement by at least {1}'.format(line['KPI'],line['Avg_diff'])
    ### Insert the remaining conditions below
    else:
        return 'No Comment'

# Then apply with axis=1 to do it row-wise
df['Comments'] = df.apply(create_comment, axis=1)

关于python - Pandas:根据多个条件生成句子并将其显示在单独的列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58637847/

相关文章:

python - 如何随机更改列表中的 bool 值

python - 按 pandas 列对比率进行分组

python - 在具有多索引的 pandas Dataframe 中,我如何按顺序过滤?

python - 如何一次对数据中的所有列进行分类? (使所有值变为高、中、低)

python - 如何在每个组中估算 Pandas 数据框中的一列

pandas - 尝试基于多索引合并 pandas 中的 2 个数据帧

python - 根据给定的日期范围获取星期一和星期日

python - 使用 Pandas 组合数据框中两行的不同部分

python - 如何在 Python 中生成 JSON 格式的 Kafka 消息

python - 第二周后每月的随机日期