数据清洗---数据整合

数据整合可以使用Pandas库中merge()函数合并数据集

import pandas as pd
#建两个数据集
df1=DataFrame({'lkey':['b','b','a','c'],'data1':range(4)})
df2=DataFrame({'rkey':['a','b','b','d'],'data2':range(4)})
#将两个数据集在key上做合并
pd.merge(df1,df2,left_on='lkey',right_on='rkey')
#how参数:连接方式
pd.merge(df1,df2,left_on='lkey',right_on='rkey',how='outer')
#使用索引
#新建数据集
left1=pd.DataFrame({'key':['a','b','a','a','b','c'],'value':range(6)})
right1=pd.DataFrame({'group_val':[3.5,7]},index=['a','b'])
#right_index
pd.merge(left1,right1,left_on='key',right_index=True,how='outer')
#自然拼接(轴向连接):纵向拼接
#concat()函数对Series或DataFrame进行轴向连接
#创建两个序列
s1=pd.Series([0,1],index=['a','b'])
s2=pd.Series([3,4],index=['c','d'])
pd.concat([s1,s2])
#将s1,s2中属性值转化为两个不同属性
pd.concat([s1,s2],axis=1)
#参数join设置基于索引的内连接结果
s3=pd.concat([s1*5,s2])
pd.concat([s1,s3],axis=1,join='inner')
#对DataFrame进行轴向连接
import numpy as np
df3=pd.DataFrame(np.arange(4).reshape((2,2)),columns=['a','b'])
df4=pd.DataFrame(np.arange(4).reshape((2,2)),columns=['a','c'])
pd.concat([df3,df4])
#ignore_index忽略行索引,重新编号
pd.concat([df3,df4],ignore_index=True)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值