網站首頁 編程語言 正文
concatenate主要作用是拼接series和dataframe的數(shù)據(jù)。
combine_first可以做來填充數(shù)據(jù)。
其中numpy和panads中都有concatenate()方法,如:np.concatenate([arr1, arr2])、pd.concat([s1, s2])
Series類型可以使用 s2 中的數(shù)值來填充 s1,如:s1.combine_first(s2)
Dataframe類型同樣可以使用 df2 中的數(shù)組來填充 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 中的數(shù)值來填充 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
'''
原文鏈接:https://laoai.blog.csdn.net/article/details/83855463
相關推薦
- 2022-05-12 python遍歷文件夾內文件并檢索文件中的中文內容
- 2021-12-09 Quartz在.NET中的使用教程_實用技巧
- 2022-11-09 SQL語句過濾條件放在on與where子句中的區(qū)別和聯(lián)系淺析_MsSql
- 2022-06-17 教你Docker安裝GitLab功能_docker
- 2022-11-14 react使用useImperativeHandle示例詳解_React
- 2022-12-08 Matlab實現(xiàn)獲取文件夾下所有指定后綴的文件_C 語言
- 2022-04-28 詳解Pandas的三大利器(map,apply,applymap)_python
- 2022-04-16 python基礎之定義類和對象詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支