用法:
pandas.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)
沿特定軸連接 pandas 對象,並沿其他軸使用可選的設置邏輯。
還可以在連接軸上添加一層分層索引,如果標簽在傳遞的軸號上相同(或重疊),這可能很有用。
- objs:Series 或 DataFrame 對象的序列或映射
如果傳遞了映射,則排序的鍵將用作
keys
參數,除非傳遞,在這種情況下將選擇值(見下文)。任何 None 對象都將被靜默刪除,除非它們都是 None 在這種情況下將引發 ValueError 。- axis:{0/'index', 1/'columns'},默認 0
要連接的軸。
- join:{‘inner’, ‘outer’},默認 ‘outer’
如何處理其他軸(或軸)上的索引。
- ignore_index:布爾值,默認為 False
如果為 True,則不要沿連接軸使用索引值。結果軸將標記為 0, ..., n - 1。如果您要連接對象,而連接軸沒有有意義的索引信息,這將非常有用。請注意,連接中仍然尊重其他軸上的索引值。
- keys:序列,默認無
如果通過了多個級別,則應包含元組。使用傳遞的鍵作為最外層構建層次索引。
- levels:序列列表,默認無
用於構造 MultiIndex 的特定級別(唯一值)。否則,它們將從 key 中推斷出來。
- names:列表,默認無
生成的分層索引中的級別名稱。
- verify_integrity:布爾值,默認為 False
檢查新的連接軸是否包含重複項。相對於實際的數據連接,這可能非常昂貴。
- sort:布爾值,默認為 False
如果
join
為 ‘outer’ 時未對齊,則對非串聯軸進行排序。這在join='inner'
時無效,它已經保留了非串聯軸的順序。- copy:布爾值,默認為真
如果為 False,則不要不必要地複製數據。
- 對象,對象類型
當沿索引 (axis=0) 連接所有
Series
時,將返回Series
。當objs
至少包含一個DataFrame
時,返回一個DataFrame
。沿列連接時 (axis=1),返回DataFrame
。
參數:
返回:
注意:
鍵、級別和名稱參數都是可選的。
可以在此處找到有關此方法如何與其他組合 pandas 對象的工具相適應的演練。
例子:
合並兩個
Series
。>>> s1 = pd.Series(['a', 'b']) >>> s2 = pd.Series(['c', 'd']) >>> pd.concat([s1, s2]) 0 a 1 b 0 c 1 d dtype:object
通過將
ignore_index
選項設置為True
來清除現有索引並在結果中將其重置。>>> pd.concat([s1, s2], ignore_index=True) 0 a 1 b 2 c 3 d dtype:object
使用
keys
選項在數據的最外層添加層次索引。>>> pd.concat([s1, s2], keys=['s1', 's2']) s1 0 a 1 b s2 0 c 1 d dtype:object
使用
names
選項標記您創建的索引鍵。>>> pd.concat([s1, s2], keys=['s1', 's2'], ... names=['Series name', 'Row ID']) Series name Row ID s1 0 a 1 b s2 0 c 1 d dtype:object
將兩個具有相同列的
DataFrame
對象組合在一起。>>> df1 = pd.DataFrame([['a', 1], ['b', 2]], ... columns=['letter', 'number']) >>> df1 letter number 0 a 1 1 b 2 >>> df2 = pd.DataFrame([['c', 3], ['d', 4]], ... columns=['letter', 'number']) >>> df2 letter number 0 c 3 1 d 4 >>> pd.concat([df1, df2]) letter number 0 a 1 1 b 2 0 c 3 1 d 4
將
DataFrame
對象與重疊列組合並返回所有內容。交叉點外的列將填充NaN
值。>>> df3 = pd.DataFrame([['c', 3, 'cat'], ['d', 4, 'dog']], ... columns=['letter', 'number', 'animal']) >>> df3 letter number animal 0 c 3 cat 1 d 4 dog >>> pd.concat([df1, df3], sort=False) letter number animal 0 a 1 NaN 1 b 2 NaN 0 c 3 cat 1 d 4 dog
將
DataFrame
對象與重疊列組合在一起,並僅返回那些通過將inner
傳遞給join
關鍵字參數來共享的對象。>>> pd.concat([df1, df3], join="inner") letter number 0 a 1 1 b 2 0 c 3 1 d 4
通過傳入
axis=1
沿 x 軸水平組合DataFrame
對象。>>> df4 = pd.DataFrame([['bird', 'polly'], ['monkey', 'george']], ... columns=['animal', 'name']) >>> pd.concat([df1, df4], axis=1) letter number animal name 0 a 1 bird polly 1 b 2 monkey george
使用
verify_integrity
選項防止結果包含重複的索引值。>>> df5 = pd.DataFrame([1], index=['a']) >>> df5 0 a 1 >>> df6 = pd.DataFrame([2], index=['a']) >>> df6 0 a 2 >>> pd.concat([df5, df6], verify_integrity=True) Traceback (most recent call last): ... ValueError:Indexes have overlapping values:['a']
相關用法
- Python pandas.concat()用法及代碼示例
- Python pandas.core.resample.Resampler.nearest用法及代碼示例
- Python pandas.core.groupby.GroupBy.nth用法及代碼示例
- Python pandas.core.groupby.SeriesGroupBy.unique用法及代碼示例
- Python pandas.core.window.rolling.Window.mean用法及代碼示例
- Python pandas.core.groupby.DataFrameGroupBy.hist用法及代碼示例
- Python pandas.core.groupby.SeriesGroupBy.nlargest用法及代碼示例
- Python pandas.core.window.expanding.Expanding.kurt用法及代碼示例
- Python pandas.core.window.expanding.Expanding.sum用法及代碼示例
- Python pandas.core.window.expanding.Expanding.median用法及代碼示例
- Python pandas.core.window.expanding.Expanding.std用法及代碼示例
- Python pandas.core.window.rolling.Window.std用法及代碼示例
- Python pandas.core.window.rolling.Rolling.aggregate用法及代碼示例
- Python pandas.core.window.expanding.Expanding.sem用法及代碼示例
- Python pandas.core.window.expanding.Expanding.corr用法及代碼示例
- Python pandas.core.groupby.DataFrameGroupBy.resample用法及代碼示例
- Python pandas.core.window.expanding.Expanding.aggregate用法及代碼示例
- Python pandas.core.resample.Resampler.transform用法及代碼示例
- Python pandas.core.window.expanding.Expanding.count用法及代碼示例
- Python pandas.core.window.rolling.Rolling.sum用法及代碼示例
注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.concat。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。