当前位置: 代码迷 >> 综合 >> pandas apply处理两个参参数 如何写case when then else end
  详细解决方案

pandas apply处理两个参参数 如何写case when then else end

热度:4   发布时间:2024-02-06 22:35:13.0

目录

pandas apply处理两个参参数 

实现sql中的 case when ..then ...else ...end


pandas apply处理两个参参数 

一个实例中,需要用两个列相除,且要判断除数不为0,求教了大佬,用如下方法解决
apply取出一行的值,再用一个自定义函数。

方式一

chengji=[[100,95,100,98],[90,98,0,94],[88,95,98,95],[99,98,97,92],[95,90,96,88],[94,94,93,77]]
data=pd.DataFrame(chengji,columns=['语文','类别','数学','政治'])
print(data.index,data.columns)
print(data,data.dtypes)data4=data.copy(deep=True)
data4=data4[['语文','数'+'学']]
#data4['数学评级']=0
def sum_test(a,b):return a+b
data4['数学'+'评级']=data4.apply(lambda x: sum_test(x['语文'],x['数学']),axis=1)
#df ['sum_value'] = df.apply(lambda x: sum_test(x['列名1'],x['列名2']), axis=1)
print("*"*50,"筛选数学大于95相加")
print(data4)data4=data.copy(deep=True)
def function(x):if x['数学']!=0:s=x['语文']/x['数学']else:s=0   return sdata4['result']=data4.apply(function,axis=1)
print(data4)

方法二

data4=data.copy(deep=True)
data4['result']=0
data4.loc[data4['数学']!=0,'result']=data4['语文']/data4['数学']
print(data4)

实现sql中的 case when ..then ...else ...end

方法一

pd_df['difficulty'] = 'Unknown'
pd_df['difficulty'][(pd_df['Time']<30) & (pd_df['Time']>0)] = 'Easy'
pd_df['difficulty'][(pd_df['Time']>=30) & (pd_df['Time']<=60)] = 'Meduim'
pd_df['difficulty'][pd_df['Time']>60] = 'Hard'

方法二

pd_df['difficulty'] = 'Unknown'
pd_df.loc[pd_df['Time'].between(0, 30, inclusive=False), 'difficulty'] = 'Easy'
pd_df.loc[pd_df['Time'].between(30, 60, inclusive=True), 'difficulty'] = 'Medium'

方法三

自定义函数

def function(x):if x > 0 and x <=30:output = 'Easy'elif:x <= 60:output = 'Medium'else:output = 'Unknown'return outputpd_df['difficulty'] = pd_df['difficulty'].apply(lambda x: function(x))

 

 

  相关解决方案