# Python進行資料提取的方法總結

``````
import numpy as np
import pandas as pd

``````
Loandata = loandata.set_index('member_id')``````

``````
loandata.ix[1303503]``````

``````
loandata.ix[:,'emp_length']``````

``````
loandata.ix[1303503,'emp_length']``````

``````
loandata.ix[[1303503,1298717],'loan_amnt']``````

``````
loandata.ix[[1303503,1298717],'loan_amnt'].sum()``````

``````
loandata.ix[1303503,['loan_amnt','annual_inc']]``````

``````
loandata.ix[1303503,['loan_amnt','annual_inc']].sum()``````

``````
loandata = loandata.set_index('issue_d')``````

``````
loandata['2016']``````

``````
loandata['2016-03']``````

``````
loandata['2016-06-16']``````

``````
loandata['2016-01':'2016-05']``````

Pandas中的`resample`函式可以完成日期的聚合工作，包括按小時維度，日期維度，月維度，季度及年的維度等等。下面我們分別說明。首先是按周的維度對前面資料表的資料進行求和。下面的程式碼中W表示聚合方式是按周，how表示資料的計算方式，預設是計算平均值，這裡設定為`sum`，進行求和計算。

``````

``````
loandata.resample('M',how=sum)``````

``````
loandata.resample('Q',how=sum)``````

``````
loandata.resample('A',how=sum)``````

``````
loandata['loan_amnt'].resample('M',how=sum).fillna(0)``````

``````
loandata[['loan_amnt','total_rec_int']].resample('M',how=[len,sum])``````

``````
loandata['2016-01':'2016-05'].resample('M',how=sum).fillna(0)``````

``````
loandata[loandata['loan_amnt']>5000].resample('M',how=sum).fillna(0)``````