pandas-14 concatenate和combine_first的用法

concatenate主要作用是拼接series和dataframe的数据。

combine_first可以做来填充数据。

其中numpy和panads中都有concatenate()方法,如:np.concatenate([arr1, arr2])、pd.concat([s1, s2])

Series类型可以使用 s2 中的数值来填充 s1,如:s1.combine_first(s2)

Dataframe类型同样可以使用 df2 中的数组来填充 df1, 如:df1.combine_first(df2)

import numpy as np
import pandas as pd
from pandas import Series, DataFrame # 设置一个随机种子,方便调试
np.random.seed(666) # Series
arr1 = np.arange(9).reshape(3, 3)
arr2 = np.arange(9).reshape(3, 3) # numpy的 concatenate 用法
print(np.concatenate([arr1, arr2]))
'''
[[0 1 2]
[3 4 5]
[6 7 8]
[0 1 2]
[3 4 5]
[6 7 8]]
''' print(np.concatenate([arr1, arr2], axis=1))
'''
[[0 1 2 0 1 2]
[3 4 5 3 4 5]
[6 7 8 6 7 8]]
''' s1 = Series([1, 2, 3], index=['A', 'B', 'C'])
s2 = Series([4, 5], index=['E', 'F'])
# 可以看出和numpy的效果一样
print(pd.concat([s1, s2]))
'''
A 1
B 2
C 3
E 4
F 5
dtype: int64
'''
# 用法和 np 一样 axis = 1, 等于增加了一列
print(pd.concat([s1, s2], axis=1))
# 但是,返回的是一个 <class 'pandas.core.frame.DataFrame'>
print(type(pd.concat([s1, s2], axis=1)))
'''
0 1
A 1.0 NaN
B 2.0 NaN
C 3.0 NaN
E NaN 4.0
F NaN 5.0
''' df1 = DataFrame(np.random.randn(4, 3), columns=['X', 'Y', 'Z'])
print(df1)
'''
X Y Z
0 0.824188 0.479966 1.173468
1 0.909048 -0.571721 -0.109497
2 0.019028 -0.943761 0.640573
3 -0.786443 0.608870 -0.931012
''' df2 = DataFrame(np.random.randn(3, 3), columns=['X', 'Y', 'A'])
print(df2)
'''
X Y A
0 0.978222 -0.736918 -0.298733
1 -0.460587 -1.088793 -0.575771
2 -1.682901 0.229185 -1.756625
''' print(pd.concat([df1, df2]))
'''
A X Y Z
0 NaN 0.824188 0.479966 1.173468
1 NaN 0.909048 -0.571721 -0.109497
2 NaN 0.019028 -0.943761 0.640573
3 NaN -0.786443 0.608870 -0.931012
0 -0.298733 0.978222 -0.736918 NaN
1 -0.575771 -0.460587 -1.088793 NaN
2 -1.756625 -1.682901 0.229185 NaN
''' # combine s1 = Series([2, np.nan, 4, np.nan], index=['A', 'B', 'C', 'D'])
s2 = Series([1, 2, 3, 4], index=['A', 'B', 'C', 'D']) # 用 s2 中的数值来填充 s1
print(s1.combine_first(s2)) '''
A 2.0
B 2.0
C 4.0
D 4.0
dtype: float64
''' df1 = DataFrame({
'X':[1, np.nan, 3, np.nan],
'Y':[5, np.nan, 7, np.nan],
'Z':[9, np.nan, 11, np.nan]
}) df2 = DataFrame({
'Z':[np.nan, 10, np.nan, 12],
'A':[1, 2, 3, 4]
}) # 功能同样是填充
print(df1.combine_first(df2))
'''
A X Y Z
0 1.0 1.0 5.0 9.0
1 2.0 NaN NaN 10.0
2 3.0 3.0 7.0 11.0
3 4.0 NaN NaN 12.0
'''

pandas-14 concatenate和combine_first的用法的更多相关文章

  1. 9.14.16 Django ORM进阶用法

    2018-9-14 14:26:45 ORM 练习题   : http://www.cnblogs.com/liwenzhou/articles/8337352.html 2018-9-14 21:1 ...

  2. Pandas | 14 统计函数

    统计方法有助于理解和分析数据的行为.可以将这些统计函数应用到Pandas的对象上. pct_change()函数 系列,DatFrames和Panel都有pct_change()函数.此函数将每个元素 ...

  3. [Python] Pandas 中 Series 和 DataFrame 的用法笔记

    目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...

  4. ASP.NET Core 6框架揭秘实例演示[14]:日志的进阶用法

    为了对各种日志框架进行整合,微软创建了一个用来提供统一的日志编程模式的日志框架.<日志的基本编程模式>以实例演示的方式介绍了日志的基本编程模式,现在我们来补充几种"进阶" ...

  5. 14. js字符串截取substring用法

    columnIds = columnIds.substring(0, columnIds.length-1);

  6. pandas.drop/isnull/fillna/astype的用法

    删除表中的某一行或者某一列更明智的方法是使用drop,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据. (1)清理无效数据 df[df.isnull()] #返回的是个 ...

  7. 14.Python bytes类型及用法

    Python 3 新增了 bytes 类型,用于代表字节串(这是本教程创造的一个词,用来和字符串对应).字符串(str)由多个字符组成,以字符为单位进行操作:字节串(bytes)由多个字节组成,以字节 ...

  8. Pandas:loc iloc ix用法

    参考:Pandas中关于 loc \ iloc \ ix 用法的理解 相同点 使用形式都是 df.xxx[ para1 , para2 ] #xxx表示loc iloc ix#df表示一个DataFr ...

  9. Pandas常用功能总结

    1.读取.csv文件 df2 = pd.read_csv('beijingsale.csv', encoding='gb2312',index_col='id',sep='\t',header=Non ...

随机推荐

  1. linux Ubuntu Centos 增加 TCP 连接数

    https://blog.csdn.net/c359719435/article/details/80300433 查看max conn: cat /proc/sys/net/core/somaxco ...

  2. MySQL:实现cumsum(累加)的功能

    需求:为实现cumsum累计求和的功能. 一张视图. SELECT 日期, 净利润 FROM daily_pnl_view; 现在希望得到,每天累计的利润是多少. SET @csum := 0;SEL ...

  3. cesium地形瓦片(Quantized-mesh)格式

    目录 1.切片规则 2.瓦片格式分析 2.1.数据头部 2.顶点数据 2.3.索引数据 2.4.扩展数据 参考资料: quantized-mesh-1.0 terrain format(用于三维可视化 ...

  4. pytorch 中conv1d操作

    参考:https://blog.csdn.net/liujh845633242/article/details/102668515 这里我重点说一下1D卷积,2D卷积很好理解,但是1D卷积就不是那么好 ...

  5. 006-nginx.conf详解-error_page 使用

    一.概述 nginx指令error_page的作用是当发生错误的时候能够显示一个预定义的uri 1.1.使用步骤 更改nginx.conf在http定义区域加入: proxy_intercept_er ...

  6. k8s记录-kubeam方式部署k8s

    参考:https://blog.csdn.net/networken/article/details/84991940 # k8s工具部署方案 # 1.集群规划 | **服务器** | || ---- ...

  7. CocosCreator TypeScript项目 (vscode 设置中文,默认调试环境设置)

    版本:2.2.1 深圳好多公司用的cocoscreator,学习一下. 这篇是如何安装,然后运行一个hello world. 一  下载 cocoscreator:https://www.cocos. ...

  8. aps.net StateServer设置

    1.在 system.web节点 加 <sessionState mode="StateServer" stateConnectionString="tcpip=1 ...

  9. java日期格式转换大全

    public class DateFormatUtils { private static Log logger = LogFactory.getLog(DateFormatUtils.class); ...

  10. makefile那些事儿

    一.好处 自动化编译,一条make命令,整个工程可以完全自动编译,make命令是构建大型项目的首选方案. makefile就像一个shell脚本一样,用来定义规则,一个名称包含一条或多条命令,在终端m ...