首页 > 其他分享 >pandas里使用map函数并且是自定义函数来实现规则的方式def

pandas里使用map函数并且是自定义函数来实现规则的方式def

时间:2023-02-14 18:13:36浏览次数:50  
标签:map elif return 函数 自定义 startswith dfASH Includes avg

 

 现在我的问题是,有一堆数据,用一些字符串开头,然后换成数字

 

 

原本我用的死办法,直接先做字典,然后用字典的内容作为map的映射规则

但是由于这些字符太多了,做字典很容易出错,导致映射出来很多NaN值,所以改用了新方法

使用函数作为映射规则,而函数再进行判断的时候,可以就用开头的字符串进行判断,准确率提升很多

#这几列看起来像是学生家长的职业信息,考虑是否需要需要数字化它,只用后面的学生家长职业的大类能否达到处理结果
#认识到处理这些数据本身,其实就是一个过程


def fx_asbg20(x):
    if x.startswith('Not applicable'): 
        return 0
    elif x.startswith('Has never worked'): 
        return 1
    elif x.startswith( 'Small Business Owner'):
        return 2
    elif x.startswith( 'Clerical Worker Includes'):
        return 3
    elif x.startswith('Service or Sales Worker'):
        return 4
    elif x.startswith('Skilled Agricultural'):
        return 5
    elif x.startswith('Craft or Trade Worker'):
        return 6
    elif x.startswith('Plant or Machine Operator'):
        return 7
    elif x.startswith('General Laborers'):
        return 8
    elif x.startswith('Corporate Manager'):
        return 9
    elif x.startswith('Professional Includes scientists'):
        return 10
    elif x.startswith('Technician or Associate Professional'):
        return 11

#dfASH_avg['ASBH20A']=dfASH_avg['ASBH20A'].map(mapASBH20)
dfASH_avg['ASBH20A']=dfASH_avg['ASBH20A'].map(fx_asbg20)

#dfASH_avg['ASBH20B']=dfASH_avg['ASBH20B'].map(mapASBH20)
dfASH_avg['ASBH20B']=dfASH_avg['ASBH20B'].map(fx_asbg20)

dfASH_avg.head()

原来写的复杂字典,费时又费力,效率还差,纪念一下

mapASBH20={'Has never worked for pay':1,
            'Small Business Owner Includes owners of small businesses (fewer than 25 employees) such as retail shops, services, resta':2,
            'Clerical Worker Includes office clerks; secretaries; typists; data entry operators;customer service cler':3,
            ' Service or Sales Worker Includes travel attendants; restaurant service workers;personal care workers; protective service workers; junior military; salespersons; street vend':4,
            'Skilled Agricultural or Fishery Worker Includes farmers; forestry workers; fishery workers; hunters and trapper':5,
            'Craft or Trade Worker Includes builders, carpenters, plumbers, electricians,metal workers; machine mechanics; handicraft workers':6,
            ' Plant or Machine Operator Includes plant and machine operators;assembly-line operators; motor-vehicle drivers':7,
            'General Laborers Includes domestic helpers and cleaners; building caretakers;messengers, porters, and doorkeepers; farm, fishery,agricultural, and construction workers':8,
            'Corporate Manager or Senior Official Includes corporate managers such as managers of large companies (25 or more employees) or managers of departments within large companies; legislators or senior government officials; senior officials of special-interest organizations; military officers':9,
            'Professional Includes scientists; mathematicians; computer scientists;architects; engineers; life science and health professionals;teachers; legal professionals; police officers; social scientists;writers and artists; religious professionals':10,
            'Technician or Associate Professional Includes science, engineering, and computer associates and technicians; life science and health technicians and assistants;teacher aides; finance and sales associate professionals;business service agents; administrative assistants':11,
            'Not applicable':0
    
    
}

 

标签:map,elif,return,函数,自定义,startswith,dfASH,Includes,avg
From: https://www.cnblogs.com/bojiandkake/p/17120387.html

相关文章

  • Linux系统Shell脚本 shell函数
    一、shell函数1、函数的作用定义较为复杂的但是需要重复使用的内容,以便再次使用可以直接调用函数节约时间,提高效率 2、函数使用步骤①首先是定义函数 ②其次是调用函......
  • 【Python21天学习挑战赛】- 函数进阶
    学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。文章目录​​1函数的初识​​​​1.1定义一个函数​​​​1.2函数的调用​​​​1.3函......
  • 字符串的输入输出函数
    字符串的输入和输出一:字符串输入gets()函数:用来读取整行输入,直至遇到换行符,然后丢弃换行符,储存其余字符,并在这些字符的末尾添加一个空字符使其成为一个C字符串。它经常......
  • vue实现自定义多选按钮
    html部分<div:class=""getSxxClass(item)v-for="(item,index)indata.sxxList":key="index"@click="sxxchangeQuery(item)"></div>js部分constdata=reactive......
  • 02-Go函数、包、条件判断、循环、switch
    1函数基础//0强调在同一个包下,无论有多少go文件,函数名和变量名不能重复函数和变量可以直接使用//把一个包,当成一个大的go文件看待go中,函数参数没有关......
  • 云小课|使用SpringBoot快速构建FunctionGraph HTTP函数
    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说)、深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云。更多精彩内容请单击......
  • 稳妥构造函数模式
    /*稳妥对象(durableobjects)*所谓稳妥对象,指的是没有公共属性,而且其方法也不引用this对象。*稳妥模式最适合在一些安全环境中(这些环境会禁止使用this和new),*或......
  • 寄生构造函数模式
    /*感觉没多大用记下得了*//**基本思维:*创建一个函数,该函数的作用仅是封装创建对象的代码,然后返回新创建的对象。*表面上看很像构造函数**/func......
  • jQuery对象和JS对象区别与转换jQuery时间绑定&入口函数&样式控制
    jQuery对象和JS对象区别与转换JQuery对象和JS对象区别与转换1.JQuery对象在操作时,更加方便。2.JQuery对象和js对象方法不通用的3.两者相互转换jq......
  • jQuery custom scrollbarjQuery自定义滚动条
    可以去GitHub上找对应的文件下载,​​https://github.com/mustache/mustache.github.com​​.点击下载压缩包下载完customscrollbar的压缩包,解压,找到里面下张图两个划线......