一些工作中常用的代码记录

2020-05-28  本文已影响0人  一颗西蓝花_

工作中,有时候需要用到代码,但是又不是每天都需要用到,难免手生,一些用法忘记了,需要重新查,浪费时间和精力,因此,打算将这些用法都记录下来,时常温习,同时,工作时候用到,直接查询自己的笔记即可。

一、批量数据导入

Case

有数据集5个

customer_profile_full_set_20200527_1.csv
customer_profile_full_set_20200527_2.csv
customer_profile_full_set_20200527_3.csv
customer_profile_full_set_20200527_4.csv
customer_profile_full_set_20200527_5.csv
customer_profile_full_set_20200527_6.csv

比较笨的办法就是一个csv一个csv的读,一个稍微方便一点的方法

import pandas as pd
path = 'customer_profile_full_set_20200527_'
customer1 = pd.read_csv(path + '1.csv')
customer2 = pd.read_csv(path + '2.csv')
customer3 = pd.read_csv(path + '3.csv')
......依此写即可

二、多表拼接

Case/场景

产品信息表,客户一共分了3个csv给,使用的时候,我们需要将这3个csv合并成一个

frame = [customer1, customer2, customer3]
customer_all = pd.concat(frame)

三、数据保存

Case/场景

将处理好的数据集,保存到本地使用
⚠️需要注意一下,当前的路径,保存数据时,如果没有指定路径,那么就会保存到当前文件路径下。

customer_all.to_csv('customer_all_20200528.csv', index = False)

四、字段拼接

场景/Case ,一个表有十个字段,col1、col2、col3、col4、col5、、、、col10,需要将字段1、2、3的值拼接起来
table['col11'] = table['col1'].map(str) + table['col2'].map(str) + table['col3'].map(str)
上一篇 下一篇

猜你喜欢

热点阅读