Datacamp
python
panda
data science
Data Manipulation with Python
作者:何彥南
Datacamp 課程: Merging DataFrames with pandas
注意:
In this chapter, you'll learn about different techniques you can use to import multiple files into DataFrames. Having imported your data into individual DataFrames, you'll then learn how to share information between DataFrames using their Indexes. Understanding how Indexes work is essential information that you'll need for merging DataFrames later in the course.
讀取資料
使用迴圈
使用一句式的for迴圈,批量讀取檔案
使用 glob
這邊我們將介紹在pandas裡index的基本操作
Pittsburgh weather data (from Datacamp)
用print 檢視資料
查看index資料
重設index
將df裡的
column
設為index
也可以依自己輸入的名字抓取對應的index,當沒有該index回傳
NaN
以上面reindex後的
w_mean3
的index為準,抓取w_max
對應index的值,並使用drop_na()丟掉空值
這邊以
w_mean
和w_max
抓取互相對應index的值,真方便呢~
Pittsburgh weather data (from Datacamp)
純量的乘法,對指定日期內的
PrecipitationIn
這個column 乘 2.54
index_lable
,column_lable
] 官方文檔
index_lable
(row名字)和column_lable
(col名字)。:
可以抓取一個範圍也可以用list一次選取多欄 ,得到絕對的溫度範圍
week1_range
這邊我們先獲取這幾天的平均溫度
week1_mean
我們想要的到一個相對的比例,但是發生錯誤了QQ
這邊我們使用pandas 內建的divide(),對
week1_range
裡的所有儲存格除以week1_mean
pct_change() 可以產生相對於前一個值得變動比率,因為是相對於前一個值,所以第一個值一定為NaN
Summer Olympic medals (from Datacamp)
銅牌獎
銀牌獎
金牌獎
把兩個資料並一起,可以看到
2247
分別為bronze和silver的加總
使用add() 也可以,這邊
Germany
和Italy
因為其中一個df有NaN 所以結果為NaN
我們可以加入
fill_value
的參數,只要其中一個友值就填補回來。
如果我們想對多個呢?
用多個add串接省去許多麻煩
Having learned how to import multiple DataFrames and share information using Indexes, in this chapter you'll learn how to perform database-style operations to combine DataFrames. In particular, you'll learn about appending and concatenating DataFrames while working with a variety of real-world datasets.
append 和concat 很類似,兩個都可以用來合併資料
建立series
使用appemnd後,可以看到資料還保有原本的index
使用reset_index(),重設index
在concat裡面我們可以直接使用ignore_index 去忽略 原本的index
一樣,我們先一下它長怎樣
用append將兩個dataframe合併起來,要注意他們兩個的column名稱一樣都是
2010 Census Population
。
這邊我們換成
Population
和unemployment
這兩個dataframe
我們將兩個dataframe併起來,但這次就沒那麼順利了,發現許多空值,因為append()預設是以
column的名稱
為準向後添加。但是這兩筆資料的column名稱不同,所以就自動補上空值。
另外,我們還可以看到這邊有index
2860
重複了
依資料合成的方向主要可以分成兩種
concat rows (
axis=0
,預設):上下依造row去合併,就是類似上面介紹的append()。
concat columns (
axis=1
):而這就是左右合併column的意思
NaN
的問題,上面兩種方式在合併時,當index沒有對應的值或是兩個dataframe長度不一,concat()會自動幫你填入空值,所以還要再對空值做對應的處理先檢視資料長怎樣
對row進行合併
使用keys,我們可以發現它產生了複合index
2013
2014
我們也可以使用loc[ ]去抓取
2014
,其中還包含月份的index
這邊
axis='columns'
與axis=1
是一樣的意思
當然,我們也可以對column使用keys,產生複合的index
我們也可以對dict,但是格式要注意。
使用arrays,他是一種在numpy下的資料型態
可以左右併起來
也可以上下併起來
這邊因為array的形狀不對,所以發生錯誤。
把他轉乘array的格式
對應的列數相同,我們可以把它左右併起來
NaN
inner
outer
再來向下concat 但是因為column之間沒有交集所以為空集合
Here, you'll learn all about merging pandas DataFrames. You'll explore different techniques for merging, and learn about left joins, right joins, inner joins, and outer joins, as well as when to use which. You'll also learn about ordered merging, which is useful when you want to merge DataFrames whose columns have natural orderings, like date-time columns.
下面我們以人口的資料和城市的資料為例
這邊我們直接使用merge,可以發現他預設的是由右邊的df中index對應的值,合到左邊的df
我們跟上面一樣用merge,但是結果卻是空的df,原因是因為兩個df的column 名字都一樣,所以merge不知道你要合哪一個column以甚麼為準。
這邊我們加入
on=
這個參數,可以看到他會以你設定on的那個column為主,去兩個df中對應的值併再一起,因為他們的column一樣,所以merge後會自動加上 _X 、_Y。
也on可以設定兩個columns。
使用
suffixes=
這個變數可以替代轉換後 _X 、_Y 的名字。
使用
left on=
和right_on
,可以設定左右兩個表分別以哪column為基準。
左右反過來也行喔,只是要注意他們對應的參數。
下面合成的方式有四種
inner:兩者交集併再一起
left:以左邊的df為主,把右邊對應的值併過來
right:以右邊的df為主,把左邊對應的值併過來
outer:輛個df的聯集
直接使用join(),他預設是left,就跟merge(how='left')一樣的意思。
join 也一樣可以使用how,這邊我們分別用
how=‘right’
、how=‘outer’
、how=‘inner’
append和concat屬於直接合併,而join和merge是依據條件去做兩個資料的串接。其中merge是最客製化的。
這邊我們使用銷售資料示範一下merge_order()的作用
timeseries
的merge
首先我們先用merge()
但我們發現sorted_values()沒有用
這時我們可以使用merge_oder(),它會自動按造日期去排。
也可以加入on 和suffixes
這邊我們使用股票和GDP資料,示範如何填補合併後ˇ的空值。
我們用merge_oder()將兩個df 依據
Data
合併,但是有些天沒有對應的資料。
於是我們可以使用
merge_odered(fill_method=)
去填補NaN
,而這邊的ffill
就是 first fill 的意思,就是依據前一個非空值去填補。