Python 熊猫:根据多个条件生成句子,并在单独的列中显示它们
这是问题的后续行动 我有一个数据帧,如下所示:Python 熊猫:根据多个条件生成句子,并在单独的列中显示它们,python,pandas,Python,Pandas,这是问题的后续行动 我有一个数据帧,如下所示: 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
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)]
为了实现上述目标,我尝试了以下方法:
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')
cond_comments=[(df['scope']='Max')和(df['BIC_diff']>0)和(df['Avg_diff']>0),
(df['scope']='Max')和(df['BIC_diff']0),
(df['scope']='Min')&(df['BIC_diff']我将创建一个函数,首先执行所有条件,然后按行应用它。这样更容易添加新条件,并查看什么条件导致什么结果
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)
def创建注释(行:dict)->str:
#列值可以像在字典中一样访问
如果(行['scope']=='Max')和(行['BIC_diff']>0)以及(行['Avg_diff']>0):
返回{0}比BIC和peer分别好{1}和{2}。格式(行['KPI'],行['BIC_diff'],行['Avg_diff']))
elif(line['scope']=='Max')和(line['BIC_diff']在您的示例中没有Avg_diff
列。这是打字错误!我已经纠正了这个问题。这很有用。尽管我做了一些修改。顺便说一句,我认为我们不需要声明line:dict-->str
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)