Python Pandas –合并,聯接和串聯

There are three main ways to combine dataFrames i.e., merging, joining and concatenating. The following examples will illustrate merging, joining and concatenation.

組合dataFrames的主要方法有三種,即合并,聯接和串聯 。 以下示例將說明合并,聯接和串聯。

Create three dataframes

創建三個數據框

import pandas as pd
df1 = pd.DataFrame({
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3'],
}, index=[0, 1, 2, 3])
df2 = pd.DataFrame({
'A': ['A4', 'A5', 'A6', 'A7'],
'B': ['B4', 'B5', 'B6', 'B7'],
'C': ['C4', 'C5', 'C6', 'C7'],
'D': ['D4', 'D5', 'D6', 'D7'],
}, index=[4, 5, 6, 7])
df3 = pd.DataFrame({
'A': ['A8', 'A9', 'A10', 'A11'],
'B': ['B8', 'B9', 'B10', 'B11'],
'C': ['C8', 'C9', 'C10', 'C11'],
'D': ['D8', 'D9', 'D10', 'D11'],
}, index=[8, 9, 10, 11])
# printing
print(df1)
print(df2)
print(df3)

Output

輸出量

    A   B   C   D
0  A0  B0  C0  D0
1  A1  B1  C1  D1
2  A2  B2  C2  D2
3  A3  B3  C3  D3
A   B   C   D
4  A4  B4  C4  D4
5  A5  B5  C5  D5
6  A6  B6  C6  D6
7  A7  B7  C7  D7
A    B    C    D
8    A8   B8   C8   D8
9    A9   B9   C9   D9
10  A10  B10  C10  D10
11  A11  B11  C11  D11

級聯 (Concatenation)

Concatenation glues/combines the dataFrames. Please note that the dimensions should match along the axis we are concatenating on.

串聯粘貼/合并dataFrames。 請注意,尺寸應沿著我們串聯的軸匹配。

Use pd.concat and pass in a list of dataFrames to concatenate together.

使用pd.concat并傳入dataFrames列表以串聯在一起。

print(pd.concat([df1,df2,df3]))
'''
Output:
A    B    C    D
0    A0   B0   C0   D0
1    A1   B1   C1   D1
2    A2   B2   C2   D2
3    A3   B3   C3   D3
4    A4   B4   C4   D4
5    A5   B5   C5   D5
6    A6   B6   C6   D6
7    A7   B7   C7   D7
8    A8   B8   C8   D8
9    A9   B9   C9   D9
10  A10  B10  C10  D10
11  A11  B11  C11  D11
'''

# specify axis=1, 
# if you want to join and merge along the concatenate
print(pd.concat([df1,df2,df3], axis = 1))
'''
Output:
A    B    C    D    A    B    C    D    A    B    C    D
0    A0   B0   C0   D0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
1    A1   B1   C1   D1  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
2    A2   B2   C2   D2  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
3    A3   B3   C3   D3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
4   NaN  NaN  NaN  NaN   A4   B4   C4   D4  NaN  NaN  NaN  NaN
5   NaN  NaN  NaN  NaN   A5   B5   C5   D5  NaN  NaN  NaN  NaN
6   NaN  NaN  NaN  NaN   A6   B6   C6   D6  NaN  NaN  NaN  NaN
7   NaN  NaN  NaN  NaN   A7   B7   C7   D7  NaN  NaN  NaN  NaN
8   NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   A8   B8   C8   D8
9   NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN   A9   B9   C9   D9
10  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  A10  B10  C10  D10
11  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  A11  B11  C11  D11
'''

In the above example, we see a bunch of missing values and that is because these data frames didn’t have values for all the indices we wanted to concatenate on. While working on concatenation and ensure that all the values are lined up correctly when joining on-axis.

在上面的示例中,我們看到了一堆丟失的值,這是因為這些數據框沒有要連接的所有索引的值。 在進行串聯時,請確保在同軸連接時所有值都正確對齊。

合并中 (Merging)

Let's create some more example dataFrames,

讓我們創建更多示例dataFrames,

left = pd.DataFrame({
'key': ['K0', 'K1', 'K2', 'K3'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']
})
right = pd.DataFrame({
'key': ['K0', 'K1', 'K2', 'K3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']
})
# printing
print(left)
print(right)
'''
Output:
key   A   B
0  K0  A0  B0
1  K1  A1  B1
2  K2  A2  B2
3  K3  A3  B3
key   C   D
0  K0  C0  D0
1  K1  C1  D1
2  K2  C2  D2
3  K3  C3  D3
'''

The merge function allows merging the dataFrames using similar logic as merging SQL tables. For instance,

合并功能允許使用與合并SQL表類似的邏輯來合并dataFrame。 例如,

# default merge on 'inner'
print(pd.merge(left,right,how='inner',on='key'))
'''
Output:
key   A   B   C   D
0  K0  A0  B0  C0  D0
1  K1  A1  B1  C1  D1
2  K2  A2  B2  C2  D2
3  K3  A3  B3  C3  D3
'''  

A little more complicated example dataFrames can be,

稍微復雜一點的示例dataFrames可以是,

left = pd.DataFrame({
'key1': ['K0', 'K0', 'K1', 'K2'],
'key2': ['K0', 'K1', 'K0', 'K1'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']
})
right = pd.DataFrame({
'key1': ['K0', 'K1', 'K1', 'K2'],
'key2': ['K0', 'K0', 'K0', 'K0'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']
})
# pass a single column 'key' or list of columns
print(pd.merge(left, right, on=['key1', 'key2']))
'''
key1 key2   A   B   C   D
0   K0   K0  A0  B0  C0  D0
1   K1   K0  A2  B2  C1  D1
2   K1   K0  A2  B2  C2  D2
'''
# merge outer (similar to outer join)
print(pd.merge(left, right, how='outer', on=['key1', 'key2']))
'''
key1 key2    A    B    C    D
0   K0   K0   A0   B0   C0   D0
1   K0   K1   A1   B1  NaN  NaN
2   K1   K0   A2   B2   C1   D1
3   K1   K0   A2   B2   C2   D2
4   K2   K1   A3   B3  NaN  NaN
5   K2   K0  NaN  NaN   C3   D3
'''
# merge right (similar to right join)
print(pd.merge(left, right, how='right', on=['key1', 'key2']))
'''
key1 key2    A    B   C   D
0   K0   K0   A0   B0  C0  D0
1   K1   K0   A2   B2  C1  D1
2   K1   K0   A2   B2  C2  D2
3   K2   K0  NaN  NaN  C3  D3
'''
# merge left (similar to left join)
print(pd.merge(left, right, how='left', on=['key1', 'key2']))
'''
key1 key2   A   B    C    D
0   K0   K0  A0  B0   C0   D0
1   K0   K1  A1  B1  NaN  NaN
2   K1   K0  A2  B2   C1   D1
3   K1   K0  A2  B2   C2   D2
4   K2   K1  A3  B3  NaN  NaN
'''

加盟 (Joining)

Joining is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Joining is very similar to merge except they are joining on the index instead of a column.

聯接是一種方便的方法,用于將兩個可能具有不同索引的DataFrame的列組合為單個結果DataFrame。 聯接與合并非常相似,只不過它們是在索引而不是列上聯接。

Create example dataFrames,

創建示例數據框,

left = pd.DataFrame({
'A': ['A0', 'A1', 'A2'],
'B': ['B0', 'B1', 'B2']
},
index = ['K0', 'K1', 'K2'])
right = pd.DataFrame({
'C': ['C0', 'C2', 'C3'],
'D': ['D0', 'D2', 'D3']
},
index = ['K0', 'K2', 'K3'])
print(left.join(right))
'''
A   B    C    D
K0  A0  B0   C0   D0
K1  A1  B1  NaN  NaN
K2  A2  B2   C2   D2
'''
print(left.join(right, how='outer'))
'''
A    B    C    D
K0   A0   B0   C0   D0
K1   A1   B1  NaN  NaN
K2   A2   B2   C2   D2
K3  NaN  NaN   C3   D3
'''

翻譯自: https://www.includehelp.com/python/python-pandas-merging-joining-and-concatenating.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542148.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542148.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542148.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Apache服務配置

1. apache 企業中常用的web服務。用來提供http://(超文本傳輸協議) 基礎信息: 主配置目錄: /etc/httpd/conf 主配置文件: /etc/httpd/conf/httpd.conf 子配置目錄: /etc/httpd/conf.d/ 子配置文…

git 怎么查看合并過來哪些代碼_git整理紛亂的歷史合并記錄

https://github.com/Epix37/Hearthstone-Deck-Tracker以上面版本庫的master分支為例父節點1SHA-1: a21142968282ae49720cf30a0f18290b2ce74b3a* remove hotkey from config if action could not be found, fix hotkey menu item name父節點2SHA-1: 86a824e8f46005db91f334dfc57…

如何安裝Genymotion虛擬機以及Genmotion的eclipse插件

---內容開始--- - 首先去genymotion的官網去下載其安裝文件 資源下載 Genymotion官網必須注冊一個賬號這個賬號安裝之后還有用的,用戶名最好用網易126郵箱注冊----我下載的是2.8.0的版本(注:注冊前先開個代理服務器不然頁面打不開下載時最好用迅雷下載這…

java system類_Java System類mapLibraryName()方法及示例

java system類系統類mapLibraryName()方法 (System class mapLibraryName() method) mapLibraryName() method is available in java.lang package. mapLibraryName()方法在java.lang包中可用。 mapLibraryName() method is used to map a given library name into a platform-…

squid服務配置(正向、反向代理)

代理: 就是代理網絡用戶去取得網絡信息。 Squid是一種用來緩沖Internet數據的軟件。安裝Squid服務實現代理緩存服務器功能。 正向代理:意思是一個位于客戶端和原始服務器之間的服務器,為了從原始服務器取得內容,客戶端向代理發送一…

家譜整站源碼php_mysql家譜表查詢某人所有后代

CREATE TABLE people (id INT(11) NOT NULL,name VARCHAR(50) NULL DEFAULT NULL,pid INT(11) NOT NULL DEFAULT 0,PRIMARY KEY (id));CREATE DEFINERroot% PROCEDURE getChildren(IN parentId INT)LANGUAGE SQLNOT DETERMINISTICCONTAINS SQLSQL SECURITY DEFINERCOMMENT 獲取…

React 入門學習筆記2

摘自阮一峰:React入門實例教程,轉載請注明出處。 一、獲取真實的DOM節點 組件并不是真實的 DOM 節點,而是存在于內存之中的一種數據結構,叫做虛擬 DOM (virtual DOM)。只有當它插入文檔以后,才會…

c語言getchar函數_C語言中帶有示例的getchar()函數

c語言getchar函數C語言中的getchar()函數 (getchar() function in C) The getchar() function is defined in the <stdio.h> header file. getchar()函數在<stdio.h>頭文件中定義。 Prototype: 原型&#xff1a; int getchar(void);Parameters: FILE *filename(f…

python及pycharm

1.python簡介&#xff1a; Python是一種計算機程序設計語言。是一種動態的、面向對象的腳本語言&#xff0c;最初被設計用于編寫自動化腳本(shell)&#xff0c;隨著版本的不斷更新和語言新功能的添加&#xff0c;越來越多被用于獨立的、大型項目的開發。 python最重要的功能&am…

anaconda如何更改環境配置_手把手教新手安裝Anaconda配置開發環境

Anaconda是針對Python的集成環境&#xff0c;它已經成為全球數千萬數據科學從業人員必備的開發工具&#xff0c;幫助人們有效地解決數據科學和機器學習相關地問題。如果你想從事數據科學和機器學習的工作&#xff0c;可以從本文開始&#xff0c;了解一下如何安裝Anaconda。1. 初…

詳解摘要認證

1. 什么是摘要認證摘要認證與基礎認證的工作原理很相似&#xff0c;用戶先發出一個沒有認證證書的請求&#xff0c;Web服務器回復一個帶有WWW-Authenticate頭的響應&#xff0c;指明訪問所請求的資源需要證書。但是和基礎認證發送以Base 64編碼的用戶名和密碼不同&#xff0c;在…

Python的基礎知識

1.注釋&#xff1a; #單行注釋ctrl / 批量注釋&#xff0c;選中需要注釋的所有行ctrl / 批量取消注釋&#xff0c;選中已經被注釋的所有行 塊注釋&#xff1a;上下各三個雙引號的部分全部被注釋 “”“ hello haha ”“”2.變量&#xff1a; 變量命名的規則&#xff1a; …

樹莓派該文件名_樹莓派:文本編輯器與文件

GNU nano是Unix系統下一款常用的文本編輯器&#xff0c;以簡單易用著稱。與之相比&#xff0c;功能更強大的Vi和Emacs編輯器&#xff0c;學習曲線比nano陡峭很多。由于nano對于一般的文本編輯來說已經足夠&#xff0c;所以我想簡單介紹一下&#xff0c;以便于更好入門。基本使用…

Java SimpleTimeZone setStartYear()方法與示例

SimpleTimeZone類setStartYear()方法 (SimpleTimeZone Class setStartYear() method) setStartYear() method is available in java.util package. setStartYear()方法在java.util包中可用。 setStartYear() method is used to set the DST (Daylight Savings Time) starting y…

報表在IBM AIX系統下resin部署

&#xfeff;&#xfeff;報表是用java開發的&#xff0c;具有良好的跨平臺性。不僅可以應用在windows、linux、操作系統&#xff0c;還可以應用在AIX等等的unix操作系統。在各種操作系統上部署過程有一些差別。下面說一下在AIX操作系統的部署的步驟。 1. 首先&#xff0c;下載…

python——if語句、邏輯運算符號

1.if條件判斷語句&#xff1a; if 要判斷的條件(True):條件成立的時候&#xff0c;要做的事情 elif 要判斷的條件(True):.... elif 要判斷的條件(True):.... else:條件不成立的時候要做的事情示例&#xff1a; 判斷學生分數等級&#xff1a; 100——90&#xff08;包括90&…

sox處理mp3_使用SoX將mp3文件拆分為TIME秒

I need to split mp3 file into slices TIME sec each. Ive tried mp3splt, but it doesnt work for me if output is less than 1 minute.Is it possible do do with:sox file_in.mp3 file_out.mp3 trim START LENGTHWhen I dont know mp3 file LENGTH解決方案You can run SoX…

Java ObjectOutputStream writeInt()方法及示例

ObjectOutputStream類writeInt()方法 (ObjectOutputStream Class writeInt() method) writeInt() method is available in java.io package. writeInt()方法在java.io包中可用。 writeInt() method is used to write the given 4 bytes of an integer value. writeInt()方法用于…

移動端適配方案(上)

轉載自:https://github.com/riskers/blog/issues/17 要搞懂移動端的適配問題&#xff0c;就要先搞明白像素和視口。 像素 在移動端給一個元素設置 width:200px 時發生了什么&#xff1f;這里的px到底是多長呢&#xff1f;像素是網頁布局的基礎&#xff0c;但是我們一直在用直覺…

python——rang函數、for、braek循環

rang函數&#xff1a; start: 計數從 start 開始。默認是從 0 開始。例如range&#xff08;5&#xff09;等價于range&#xff08;0&#xff0c; 5&#xff09;; stop: 計數到 stop 結束&#xff0c;但不包括 stop。例如&#xff1a;range&#xff08;0&#xff0c; 5&#xf…