gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:00:48 27 4
gpt4 key购买 nike

这是问题 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com