Python 如何在lambda.apply函数中应用嵌套for循环来迭代dataframe中的列,预期输出如下图所示

Python 如何在lambda.apply函数中应用嵌套for循环来迭代dataframe中的列,预期输出如下图所示,python,dataframe,lambda,apply,Python,Dataframe,Lambda,Apply,如何在lambda.apply函数中应用嵌套for循环来迭代dataframe中的列,预期输出如下图所示 import pandas as pd from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from datetime import date, datetime, timedelta import smtplib # Setup data firstProductSet =

如何在lambda.apply函数中应用嵌套for循环来迭代dataframe中的列,预期输出如下图所示

import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import date, datetime, timedelta
import smtplib

# Setup data
firstProductSet = {'Product1': ['Computer','Phone','Printer','Desk'],'Price1': [1200,800,200,350],'Price2': [11,11,11,11],'Price3': [1,800,1,1],'Price4': [111,111,11,111]}
df1 = pd.DataFrame(firstProductSet,columns= ['Product1', 'Price1','Price2','Price3','Price4'])
尝试结果

result_2 = (df1.style.apply(lambda x:["color: red" if ((i == 3 and v != x.iloc[1]) or (i == 4 and v != x.iloc[2])) else "" for i, v in enumerate(x)], axis=1))

html=result_2.hide_index().render()

基本上尝试使用另一个for循环来动态检查列比较,而不是OR条件df={'Product1':['Computer','Phone','Printer','Desk'],'Price1':[1200800200350],'Price2':[11,11,11,11],'Price3':[1800,1,1],'Price4':[11111111111]}df1=pd.DataFrame(df,columns=['Product1'、'Price1'、'Price2'、'Price3'、'Price4'])result=(df1.style.apply(lambda x:[“color:red”if((i==3和v!=x.iloc[1])或(i==4和v!=x.iloc[2])else“”,用于枚举(x)],axis=1)中的i,v)html=result\u 2.hide\u index().render()#发送电子邮件检查格式请将您的样本数据输入问题文本中,评论用于讨论和澄清,不显示代码wellhad难以发布此内容,