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. k8s相关

    卸载kubernetes-dashboard kubectl get secret,sa,role,rolebinding,services,deployments --namespace=kube- ...

  2. 利用python实现汉字转拼音

    安装:pip install pypinyin import pypinyin # 不带声调的(style=pypinyin.NORMAL) def pinyin(word): s = '' for ...

  3. php curl 转为 x-www-form-urlencoded 方式

    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); fr ...

  4. DB2数据库基础

    一.DB2数据库安装教程 DB2安装教程:https://jingyan.baidu.com/article/e75057f2f59ef9ebc91a8905.html 二.DB2常用命令 1. 打开 ...

  5. Systemd笔记

    Systemd管理的启动脚本位于 /usr/lib/systemd/system/ 下 Systemd启动顺序约定为: 当前服务满足启动条件, 则立即创建进程进行并行启动, 启动条件指服务的依赖关系( ...

  6. 【C++】C++中的动态内存解析

    目录结构: contents structure [-] 动态内存和智能指针 使用shared_ptr管理内存 使用new直接管理内存 shared_ptr和new结合使用 unique_ptr we ...

  7. java中ExecutorService使用多线程处理业务

    ExecutorService executorService = Executors.newFixedThreadPool(5); List<CancelApprovalCallable> ...

  8. centos7.5环境下elasticserch5.6.15集群升级6.8.4

    节点的角色分片: node01 eus_mp_web01 : master,false node,false, ingest,true node02 eus_mp_es01 : master,true ...

  9. LeetCode_448. Find All Numbers Disappeared in an Array

    448. Find All Numbers Disappeared in an Array Easy Given an array of integers where 1 ≤ a[i] ≤ n (n  ...

  10. mysqldump导出完整sql脚本

    #导出某个数据库--结构+数据 shell>mysqldump -h192.168.161.124 -uroot -pxxxxxx --opt db_name |gzip -9 > /db ...