pandas-14 concatenate和combine_first的用法
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的用法的更多相关文章
- 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 ...
- Pandas | 14 统计函数
统计方法有助于理解和分析数据的行为.可以将这些统计函数应用到Pandas的对象上. pct_change()函数 系列,DatFrames和Panel都有pct_change()函数.此函数将每个元素 ...
- [Python] Pandas 中 Series 和 DataFrame 的用法笔记
目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...
- ASP.NET Core 6框架揭秘实例演示[14]:日志的进阶用法
为了对各种日志框架进行整合,微软创建了一个用来提供统一的日志编程模式的日志框架.<日志的基本编程模式>以实例演示的方式介绍了日志的基本编程模式,现在我们来补充几种"进阶" ...
- 14. js字符串截取substring用法
columnIds = columnIds.substring(0, columnIds.length-1);
- pandas.drop/isnull/fillna/astype的用法
删除表中的某一行或者某一列更明智的方法是使用drop,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据. (1)清理无效数据 df[df.isnull()] #返回的是个 ...
- 14.Python bytes类型及用法
Python 3 新增了 bytes 类型,用于代表字节串(这是本教程创造的一个词,用来和字符串对应).字符串(str)由多个字符组成,以字符为单位进行操作:字节串(bytes)由多个字节组成,以字节 ...
- Pandas:loc iloc ix用法
参考:Pandas中关于 loc \ iloc \ ix 用法的理解 相同点 使用形式都是 df.xxx[ para1 , para2 ] #xxx表示loc iloc ix#df表示一个DataFr ...
- Pandas常用功能总结
1.读取.csv文件 df2 = pd.read_csv('beijingsale.csv', encoding='gb2312',index_col='id',sep='\t',header=Non ...
随机推荐
- yapi内网部署 centos
1.部署方案 官方说明: https://hellosean1025.github.io/yapi/devops/index.html 2.需要注意的点 (1)在centos等服务启上最好使用“命令行 ...
- shebang是啥
在计算领域中,Shebang(也称为 Hashbang )是一个由井号和叹号构成的字符序列 #! ,其出现在文本文件的第一行的前两个字符. 在文件中存在 Shebang 的情况下,类 Unix 操作系 ...
- Android设置顶部banner背景透明度时影响全局背景问题
项目中用到滑动界面使顶部title栏背景渐隐渐现的效果,即初始不滑动时的透明度为0,用了bannerle.getBackground().setAlpha(0); 但使用这个方法设置透明度是管用,返回 ...
- cheerio 服务器端的jquery
cheerio https://cheerio.js.org/ Fast, flexible, and lean implementation of core jQuery designed spec ...
- Vue打包发布到Tomcat后,刷新报错404解决方法
在应用下面加 WEB-INF 建 web.xml 内容如下 <?xml version="1.0" encoding="ISO-8859-1"?> ...
- 怎样在sql server profiler跟踪时只显示自己关心的内容
当我们想知道.net程序到底执行了哪些SQL的时候,通常会使用sql server profiler,但是如果不加设置,我们程序执行的sql通常会被系统的SQL淹没,通过以下的方法,可以只显示我们需要 ...
- simplexml_load_string 转换xml为数组
php simplexml_load_string 函数可以很简单转换 xml 字符串为 SimpleXMLElement 对象 但是,对象的值不好取,例如: result => SimpleX ...
- c#实现定时任务(Timer)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- mysql笔记7--一句查询语句的过程
1 sql语句示例 select *from A where id=1 2 mysql基本架构图 (1)Mysql分为Server层和引擎层两个部分 (2)Server层包括连接器,查询缓存,分析器, ...
- 【idea】设置背景颜色
File->Settings->Editor->Color Scheme->General->Text->Default text->Background