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平台Boost 1.6.7的编译方法

    boost库下载地址:https://dl.bintray.com/boostorg/release/ 编译: 1. 获得bjam (1) # cd /usr/src/boost_1_67_0 (2) ...

  2. dubbo源码分析之过滤器Filter-12

    https://blog.csdn.net/luoyang_java/article/details/86682668 Dubbo 是阿里巴巴开源的一个高性能优秀的服务框架,使得应用可通过高性能的 R ...

  3. MySQL索引原理(二)

    MySQL索引原理 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中.索引是数据库中专门用于帮助用户快速查询数据的一种数据结构. ...

  4. centos上传命令

    首先安装lrzsz # yum -y install lrzsz 1.上传文件,执行命令rz,会跳出文件选择窗口,选择好文件,点击确认即可. # rz 运行rz命令后弹出选择文件窗口,找到要上传的文件 ...

  5. 通过mitmproxy爬取APP的数据

    安装: https://mitmproxy.org/ 小米安装证书 设置->系统安全->从存储设备安装->选择*.pem文件 模拟器安装证书 (请从C:\Users\John\.mi ...

  6. Xamarin.FormsShell基础教程(8)Shell的模版构成

    Xamarin.FormsShell基础教程(8)Shell的模版构成 Shell模版创建的页面包含内容页.标签栏.侧滑菜单.搜索框四部分.下面依次介绍这几个部分. 内容页:内容页就是为用户呈现内容的 ...

  7. Ant Design Pro 子界面传值

  8. syncronizationcontext

    FormDesign.cs       增加变量定义  public string testName { get; set; } TaskWrapper.cs string tmpTest = &qu ...

  9. Windows Server 2008 R2怎样设置自动登陆(登录)

    方法一: 打开电脑“菜单”,右键点击“运行”,在对话框输入“control userpasswords2”,点击“确定”. 弹出的窗口取消勾选“要使用本机用户必须输入用户名和密码”,取消后点击“确定” ...

  10. 20190726_安装CentOS7minimal版本后需要做的优化和配置

    20190726_安装CentOS7minimal版本后需要做的优化和配置 CentOS系统镜像下载地址:https://www.centos.org/ CentOS的Minimal(最小化安装版本) ...