Pandas-高级处理(二):连接与修补【concat(参数:axis、join、keys)、combine_first(根据index,df1的空值被df2替代)】

本文主要是介绍Pandas-高级处理(二):连接与修补【concat(参数:axis、join、keys)、combine_first(根据index,df1的空值被df2替代)】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、连接(concat):沿轴执行连接操作

pd.concat([data1, data2], axis=1):按照行或列进行连接操作:

  • axis=0为列索引;
  • axis=1为行索引;

比如我们将刚才处理好的one-hot编码与原数据连接

1、参数:axis

import pandas as pd# 连接:concats1 = pd.Series([1, 2, 3])
s2 = pd.Series([2, 3, 4])s3 = pd.Series([1, 2, 3], index=['a', 'c', 'h'])
s4 = pd.Series([2, 3, 4], index=['b', 'e', 'd'])print("s1 = \n", s1)
print('-' * 50)
print("s2 = \n", s2)
print('-' * 50)
print("s3 = \n", s3)
print('-' * 50)
print("s4 = \n", s4)
print('-' * 200)# 默认axis=0,行+行
data1 = pd.concat([s1, s2])
print("data1 = pd.concat([s1,s2]) = \n", data1)
print('-' * 200)data2 = pd.concat([s3, s4]).sort_index()
print("data2 = pd.concat([s3,s4]).sort_index() = \n", data2)
print('-' * 200)# axis=1,列+列,成为一个Dataframe
data3 = pd.concat([s3, s4], axis=1)
print("data3 = pd.concat([s3,s4], axis=1) = \n", data3)
print('-' * 200)

打印结果:

s1 = 0    1
1    2
2    3
dtype: int64
--------------------------------------------------
s2 = 0    2
1    3
2    4
dtype: int64
--------------------------------------------------
s3 = a    1
c    2
h    3
dtype: int64
--------------------------------------------------
s4 = b    2
e    3
d    4
dtype: int64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data1 = pd.concat([s1,s2]) = 0    1
1    2
2    3
0    2
1    3
2    4
dtype: int64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data2 = pd.concat([s3,s4]).sort_index() = a    1
b    2
c    2
d    4
e    3
h    3
dtype: int64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data3 = pd.concat([s3,s4], axis=1) = 0    1
a  1.0  NaN
c  2.0  NaN
h  3.0  NaN
b  NaN  2.0
e  NaN  3.0
d  NaN  4.0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Process finished with exit code 0

2、参数:join

import pandas as pd# 连接方式:joins5 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
s6 = pd.Series([2, 3, 4], index=['b', 'c', 'd'])
print("s5 = \n", s5)
print('-' * 50)
print("s6 = \n", s6)
print('-' * 200)data1 = pd.concat([s5, s6], axis=1)
print("data1 = pd.concat([s5,s6], axis= 1) = \n", data1)
print('-' * 200)# join:{'inner','outer'},默认为“outer”。如何处理其他轴上的索引。outer为联合和inner为交集。
data3 = pd.concat([s5, s6], axis=1, join='inner')
print("data3 = pd.concat([s5,s6], axis= 1, join='inner') = \n", data3)
print('-' * 200)

打印结果:

s5 = a    1
b    2
c    3
dtype: int64
--------------------------------------------------
s6 = b    2
c    3
d    4
dtype: int64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data1 = pd.concat([s5,s6], axis= 1) = 0    1
a  1.0  NaN
b  2.0  2.0
c  3.0  3.0
d  NaN  4.0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data3 = pd.concat([s5,s6], axis= 1, join='inner') = 0  1
b  2  2
c  3  3
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Process finished with exit code 0

3、参数:keys

import pandas as pd# 连接方式:joins5 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
s6 = pd.Series([2, 3, 4], index=['b', 'c', 'd'])
print("s5 = \n", s5)
print('-' * 50)
print("s6 = \n", s6)
print('-' * 200)# 覆盖列名
# keys:序列,默认值无。使用传递的键作为最外层构建层次索引
sre1 = pd.concat([s5, s6], keys=['one', 'two'])
print("sre1 = \n{0} \ntype(sre1) = {1}".format(sre1, type(sre1)))
print('-' * 50)
print("sre.index = \n", sre1.index)
print('-' * 200)# axis = 1, 覆盖列名
sre2 = pd.concat([s5, s6], axis=1)
print("sre2 = \n{0} \ntype(sre2) = {1}".format(sre2, type(sre2)))
print('-' * 50)
sre3 = pd.concat([s5, s6], axis=1, keys=['one', 'two'])
print("sre3 = \n{0} \ntype(sre3) = {1}".format(sre3, type(sre3)))

打印结果:

s5 = a    1
b    2
c    3
dtype: int64
--------------------------------------------------
s6 = b    2
c    3
d    4
dtype: int64
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sre1 = 
one  a    1b    2c    3
two  b    2c    3d    4
dtype: int64 
type(sre1) = <class 'pandas.core.series.Series'>
--------------------------------------------------
sre.index = MultiIndex([('one', 'a'),('one', 'b'),('one', 'c'),('two', 'b'),('two', 'c'),('two', 'd')],)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sre2 = 0    1
a  1.0  NaN
b  2.0  2.0
c  3.0  3.0
d  NaN  4.0 
type(sre2) = <class 'pandas.core.frame.DataFrame'>
--------------------------------------------------
sre3 = one  two
a  1.0  NaN
b  2.0  2.0
c  3.0  3.0
d  NaN  4.0 
type(sre3) = <class 'pandas.core.frame.DataFrame'>Process finished with exit code 0

二、修补(combine_first)

  • 根据index,df1的空值被df2替代

  • 如果df2的index多于df1,则更新到df1上,比如index=[‘a’,1]

  • update,直接df2覆盖df1,相同index位置

import numpy as np
import pandas as pd# 修补 pd.combine_first()df1 = pd.DataFrame([[np.nan, 3., 5.], [-4.6, np.nan, np.nan], [np.nan, 7., np.nan]])
df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5., 1.6, 4]], index=[1, 2])
df3 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5., 1.6, 4]], index=['a', 1])
print("df1 = \n", df1)
print('-' * 50)
print("df2 = \n", df2)
print('-' * 200)# 根据index,df1的空值被df2替代
data1 = df1.combine_first(df2)
print("data1 = \n", data1)
print('-' * 200)# 如果df2的index多于df1,则更新到df1上,比如index=['a',1]
data2 = df1.combine_first(df3)
print("data2 = \n", data2)
print('-' * 200)# update,直接df2覆盖df1,相同index位置
df1.update(df2)
print("df1 = \n", df1)

打印结果:

df1 = 0    1    2
0  NaN  3.0  5.0
1 -4.6  NaN  NaN
2  NaN  7.0  NaN
--------------------------------------------------
df2 = 0    1    2
1 -42.6  NaN -8.2
2  -5.0  1.6  4.0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data1 = 0    1    2
0  NaN  3.0  5.0
1 -4.6  NaN -8.2
2 -5.0  7.0  4.0
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data2 = 0    1    2
0   NaN  3.0  5.0
1  -4.6  1.6  4.0
2   NaN  7.0  NaN
a -42.6  NaN -8.2
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
df1 = 0    1    2
0   NaN  3.0  5.0
1 -42.6  NaN -8.2
2  -5.0  1.6  4.0Process finished with exit code 0

这篇关于Pandas-高级处理(二):连接与修补【concat(参数:axis、join、keys)、combine_first(根据index,df1的空值被df2替代)】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1128673

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty