目录

pandas apply处理两个参参数 

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


pandas apply处理两个参参数 

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

方式一

import pandas as pd
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 s

data4['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 output

pd_df['difficulty'] = pd_df['difficulty'].apply(lambda x: function(x))

 

 

 
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐