一.shift()
df.shift(periods=1, freq=None, axis=0)
df = pd.DataFrame(np.arange(1,17).reshape(4,4),columns=['A','B','C','D'],index =['a','b','c','d'])
print(df)
A B C D
a 1 2 3 4
b 5 6 7 8
c 9 10 11 12
d 13 14 15 16
print(df.shift(3))
A B C D
a NaN NaN NaN NaN
b NaN NaN NaN NaN
c NaN NaN NaN NaN
d 1.0 2.0 3.0 4.0
print(df.shift(2,axis = 1))
A B C D
a NaN NaN 1.0 2.0
b NaN NaN 5.0 6.0
c NaN NaN 9.0 10.0
d NaN NaN 13.0 14.0
print(df.shift(-1))
A B C D
a 5.0 6.0 7.0 8.0
b 9.0 10.0 11.0 12.0
c 13.0 14.0 15.0 16.0
d NaN NaN NaN NaN
df = pd.DataFrame(np.arange(1,17).reshape(4,4),columns=['A','B','C','D'],index =pd.date_range('10/1/2018','10/4/2018'))
print(df)
A B C D
2018-10-01 1 2 3 4
2018-10-02 5 6 7 8
2018-10-03 9 10 11 12
2018-10-04 13 14 15 16
print(df.shift(freq=datetime.timedelta(1)))
A B C D
2018-10-02 1 2 3 4
2018-10-03 5 6 7 8
2018-10-04 9 10 11 12
2018-10-05 13 14 15 16
print(df.shift(freq=datetime.timedelta(-1)))
A B C D
2018-09-30 1 2 3 4
2018-10-01 5 6 7 8
2018-10-02 9 10 11 12
2018-10-03 13 14 15 16
shift如字面義,移動,
函數中的幾個參數意義如下:
period:表示移動的幅度,可以是正數,也可以是負數,默認值是1,1就表示移動一次,移動之後沒有對應值的,就賦值爲NaN。
freq: DateOffset, timedelta, or time rule string,可選參數,默認值爲None,只適用於時間序列
axis: 軸向。0表示行向移動(上下移動),1表示列向移動(左右移動)
period與freq的區別:
period移動時,只移動數據,行列索引不移動;
freq移動時,只移動索引,數據不變,且只在索引是時間時生效
period移動時的理解:
整個數據塊移動,比如向下移動3行時,整個數據塊向下移動,原本下三行就移出了我們規定的4*4的矩陣,所以原本下三行的數據就不可見了,而原本第一行上面是沒有數據的,下移後依然爲空,只是pandas裏空數據一般以NaN佔位
freq移動時的理解:
時間的移動自然是前一天後一天這樣,所以,1就是後移一天,-1就是前移一天
二.map(),apply(),applymap()
def change(x):
if x > 2:
return 'big'
return 'small'
s = pd.Series([1, 2, 3, np.nan])
frame = pd.DataFrame(np.arange(1, 17).reshape(4,4), columns=list('abcd'), index=['A', 'B', 'C', 'D'])
s2 = s.map(lambda x: change(x),na_action=None)
s3 = s.map(lambda x: change(x).format(x),na_action='ignore')
print(s2,s3)
0 small
1 small
2 big
3 small
dtype: object
0 small
1 small
2 big
3 NaN
dtype: object
print(frame)
a b c d
A 1 2 3 4
B 5 6 7 8
C 9 10 11 12
D 13 14 15 16
print(frame.apply(lambda x:x.max()/x.min()))
a 13.0
b 7.0
c 5.0
d 4.0
dtype: float64
print(frame.applymap(change))
a b c d
A small small big big
B big big big big
C big big big big
D big big big big
Series.map(arg, na_action=None)
當na_action爲None時,NaN會被傳遞到函數中,如果ignore,則直接傳遞Na值,而不將它傳遞到函數
Series.apply(func, convert_dtype=True, args=(), **kwds)
func:function
convert_dtype:boolean,default True嘗試找到更好的dtype元素功能結果。如果爲False,則保留爲dtype = object
args:tuple除了值之外,還要傳遞位置參數