python数据清洗常用:工作总结1

每份工作总结写20条

1. 对一列数据进行操作

a['create_tm'].map(lambda x:x[:10])

2.将一列数据变为时间格式:

pd.to_datetime(a['create_tm'].map(lambda x:x[:10]))

3.将一列数据的索引重置

   从1开始索引

a.index = range(1,len(a)+1)
a.index = range(len(a))

4.使用for循环进行遍历

for i in  range(len(a)):

5.将list转换成dataFrame进行输出

result_last = pd.concat(a)

6.对一些需要的列进行重新命名

result_last.rename(columns={'ground_task_id': 'taskId', 'src_zone_code': 'src_code'}, inplace=True)

7. 将所有数据按照既定的字段进行分组

a.groupby(by= a.task_id)

8.分组之后是groupby的数据类型,没办法直接使用,将数据转换成为list的格式直接使用

a_list = list(a.groupby(by= a.task_id))

9.通过分组之后的list转换成为字典格式使用数据

task_id_dict = dict(list(a.groupby(by= a.task_id)))

10.空值的判断方式一定要正确:

例如:原本的dataFrame中有空值,那么判断方式就需要用pd.isnull或者pd.notnull

例如,需要判断取出一列中的非空项

a['depart_time'] = a[['actual_depart_tm','plan_depart_tm']].apply(
    lambda x:x['actual_depart_tm'] if pd.notnull(x['actual_depart_tm']) else  'aaaaaa',axis=1)

11.将字典转换成dataFrame进行csv文件输出时,(暂时是这样做的)

(1)现将字典转换成为list

handled_task = list(dict.values())

(2)将list转换成为dataFrame

dataFrame = pd.concat(handled_task)

12.判断某个特定字段是否为空

(1)把该字段转换为str格式,直接判断该str是否等于nan

        if str(each_line_data['depart_time'][i]) == 'nan':
            each_line_data['depart_time'][i] = each_line_data['arrive_time'][i-1]

(2)直接使用dataFrame的自带方法判断,需要写try

            try:
                np.isnan(each_line_data['depart_time'][i])
            except TypeError:
                each_line_data['depart_time'][i] = each_line_data['arrive_time'][i - 1]

 

13.选取一列中包含指定状态的值(isin)

vehicle = a[a['state'].isin(['已完成'])]

14.把一列中的重复字段删除,得出这一列中的非重复字段

a['restore_state'].drop_duplicates()

15.注意进行条件筛选的时候每个条件可能都需要加括号

execution_vehicle = a.loc[(a['vehicle_serial'] == finished_vehicle['vehicle_serial'][j]) & (a['restore_state'] == '执行中')]

16.注意python的逻辑判断,用 &| ,目前使用and和or还没有正确过,会报布尔值什么鬼的错误

17.创建了新的空的dataFrame并赋予需要的值,注意中括号的添加方法

result = result.append(pd.DataFrame({'task_id':[late_task['task_id'][i]],'depart_time':[late_task['actual_depart_time'][i]],'arrive_time':[late_task['actual_arrive_time'][i]],'can_use_vehicle_serial':[unassigned_task['vehicle_serial'][k]],'can_use_vehicle_depart_time':[unassigned_task['plan_depart_tm'][k]], 'can_use_vehicle_arrive_time':[unassigned_task['plan_arrive_tm'][k]]}),ignore_index=True,sort=True)
                   

18.读入文件时解决编码报错的问题:

path = 'D://Document And Settings3/lqz/Desktop/Walden.txt'
file = open(path, encoding='gb18030', errors='ignore')

19.使用正则表达式匹配日期

1   pattern1 = r'\d{4}-\d{1,2}-\d{1,2}\s+\d{1,2}:\d{1,2}'

2   s.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);

用于对日期进行匹配的,格式为 年/月/日 或者 年-月-日
\d 表示任意一个位的数字(0~9);
\d{1,4} 表示这个数字可为 1~4 个长度,比如 2、09、2010;\d{4}表示数字有4位
\d{1,2} 表示 1~2 个长度
-|\/ 表示匹配 - 符号或者 / 符号
\2 这里是匹配 (-|\/) 这里的字符,也就是,如果前面用了 - ,那这里也匹配 -,如果前面是 / ,那这里也匹配 /。
^ 文本开头位置
$ 文本结束位置
现在就可知道,这个正则表达式的匹配的就是:
1~4个位的数字/1~2位的数字/1~2位的数字 或者
1~4个位的数字-1~2位的数字-1~2位的数字
 

20.关于时间的转换问题:(使用datetime.datetime.strptime基本可以姐姐任何时间想转换为2017-04-23 05:15:05的格式)

newsTime='Sun, 23 Apr 2017 05:15:05 GMT'
#Apr,Sept等月份简写的占位符是%b, 而03,04这些月份数字的占位符是%m
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
newsTime = datetime.datetime.strptime(newsTime,GMT_FORMAT)
print(newsTime)

但是感觉datetime.datetime.strptime这个函数好像只能对时间数据进行一个一个的处理,不能整列整列的处理,所以,需要对一整列数据进行处理的话可能会需要遍历整个数据。

 

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值