|NO.Z.00042|——————————|BigDataEnd|——|Hadoop&Python.v06|——|Arithmetic.v06|Pandas数据分析库:Pandas数据集成|
一、数据集成:pandas 提供了多种将 Series、DataFrame 对象组合在⼀起的功能
### --- concat数据串联
~~~ # concat数据串联
import pandas as pd
import numpy as np
df1 = pd.DataFrame(data = np.random.randint(0,150,size = [10,3]), # 计算机科⽬的考试成绩
index = list('ABCDEFGHIJ'), # ⾏标签,⽤户
columns=['Python','Tensorflow','Keras']) # 考试科⽬
df2 = pd.DataFrame(data = np.random.randint(0,150,size = [10,3]), # 计算机科⽬的考试成绩
index = list('KLMNOPQRST'), # ⾏标签,⽤户
columns=['Python','Tensorflow','Keras']) # 考试科⽬
df3 = pd.DataFrame(data = np.random.randint(0,150,size = (10,2)),
index = list('ABCDEFGHIJ'),
columns=['PyTorch','Paddle'])
pd.concat([df1,df2],axis = 0) # df1和df2⾏串联,df2的⾏追加df2⾏后⾯
df1.append(df2) # 在df1后⾯追加df2
pd.concat([df1,df3],axis = 1) # df1和df2列串联,df2的列追加到df1列后⾯
### --- 插入
~~~ # 插⼊
import numpy as np
import pandas as pd
df = pd.DataFrame(data = np.random.randint(0,151,size = (10,3)),
index = list('ABCDEFGHIJ'),
columns = ['Python','Keras','Tensorflow'])
df.insert(loc = 1,column='Pytorch',value=1024) # 插⼊列
df
# 对⾏的操作,使⽤追加append,默认在最后⾯,⽆法指定位置
# 如果想要在指定位置插⼊⾏:切割-添加-合并
### --- oin SQL⻛格合并
~~~ # oin SQL⻛格合并
~~~ # 数据集的合并(merge)或连接(join)运算是通过⼀个或者多个键将数据链接起来的。这些运算是关
~~~ # 系型数据库的核⼼操作。pandas的merge函数是数据集进⾏join运算的主要切⼊点。
import pandas as pd
import numpy as np
# 表⼀中记录的是name和体重信息
df1 = pd.DataFrame(data = {'name':
['softpo','Daniel','Brandon','Ella'],'weight':[70,55,75,65]})
# 表⼆中记录的是name和身⾼信息
df2 = pd.DataFrame(data = {'name':
['softpo','Daniel','Brandon','Cindy'],'height':[172,170,170,166]})
df3 = pd.DataFrame(data = {'名字':
['softpo','Daniel','Brandon','Cindy'],'height':[172,170,170,166]})
# 根据共同的name将俩表的数据,进⾏合并
pd.merge(df1,df2,
how = 'inner', # 内合并代表两对象交集
on = 'name')
pd.merge(df1,df3,
how = 'outer', # 全外连接,两对象并集
left_on = 'name', # 左边DataFrame使⽤列标签 name进⾏合并
right_on = '名字') # 右边DataFrame使⽤列标签 名字进⾏合并
# 创建10名学⽣的考试成绩
df4 = pd.DataFrame(data = np.random.randint(0,151,size = (10,3)),
index = list('ABCDEFHIJK'),
columns=['Python','Keras','Tensorflow'])
# 计算每位学⽣各科平均分,转换成DataFrame
score_mean = pd.DataFrame(df4.mean(axis = 1).round(1),columns=['平均分'])
# 将平均分和df3使⽤merge进⾏合并,它俩有共同的⾏索引
pd.merge(left = df4,right = score_mean,
left_index=True, # 左边DataFrame使⽤⾏索引进⾏合并
right_index=True) # 右边的DataFrame使⽤⾏索引进⾏合并
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
bdv027-python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了