data=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
data
Out[14]:
A B C D
2019-03-011.866219-1.0030572.526899-0.5633432019-03-02-1.4136261.116727-0.470054-0.5679302019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.4124362019-03-05-1.8547860.9800920.797543-0.432381
data.shape
Out[15]:(6,4)
data.values
Out[16]:
array([[1.86621859,-1.0030566,2.52689932,-0.56334339],[-1.41362647,1.11672695,-0.47005354,-0.56793016],[0.11220208,-0.62181257,-0.65821206,-0.53731156],[-0.5678393,2.17593279,1.12604991,-0.412436],[-1.85478576,0.98009218,0.79754332,-0.43238061],[-0.28618627,0.7689724,0.75578607,0.15118955]])
字典时间戳
d={
'A':1,'B':pd.Timestamp('20130301'),'C':range(4),'D':np.arange(4)}
d
Out[19]:{
'A':1,'B': Timestamp('2013-03-01 00:00:00'),'C':range(0,4),'D': array([0,1,2,3])}
构造二维形式
df=pd.DataFrame(d)
df
Out[21]:
A B C D
012013-03-0100112013-03-0111212013-03-0122
查看数据和排序修改
data.head(2)
Out[22]:
A B C D
2019-03-011.866219-1.0030572.526899-0.5633432019-03-02-1.4136261.116727-0.470054-0.567930
data.tail()
Out[23]:
A B C D
2019-03-02-1.4136261.116727-0.470054-0.5679302019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.4124362019-03-05-1.8547860.9800920.797543-0.4323812019-03-06-0.2861860.7689720.7557860.151190
data.index
Out[24]:
DatetimeIndex(['2019-03-01','2019-03-02','2019-03-03','2019-03-04','2019-03-05','2019-03-06'],
dtype='datetime64[ns]', freq='D')
data.columns
Out[25]: Index(['A','B','C','D'], dtype='object')
data.describe()
Out[26]:
A B C D
count 6.0000006.0000006.0000006.000000
mean -0.3573360.5694760.679669-0.393702
std 1.3093621.1815771.1612920.275140min-1.854786-1.003057-0.658212-0.56793025%-1.202180-0.274116-0.163594-0.55683550%-0.4270130.8745320.776665-0.48484675%0.0126051.0825681.043923-0.417422max1.8662192.1759332.5268990.151190
data.T
Out[27]:2019-03-012019-03-022019-03-032019-03-042019-03-052019-03-06
A 1.866219-1.4136260.112202-0.567839-1.854786-0.286186
B -1.0030571.116727-0.6218132.1759330.9800920.768972
C 2.526899-0.470054-0.6582121.1260500.7975430.755786
D -0.563343-0.567930-0.537312-0.412436-0.4323810.151190
安列排序
data.sort_index(axis=1)
Out[28]:
A B C D
2019-03-011.866219-1.0030572.526899-0.5633432019-03-02-1.4136261.116727-0.470054-0.5679302019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.4124362019-03-05-1.8547860.9800920.797543-0.4323812019-03-06-0.2861860.7689720.7557860.151190
data.sort_index(axis=1,ascending=False)
Out[29]:
D C B A
2019-03-01-0.5633432.526899-1.0030571.8662192019-03-02-0.567930-0.4700541.116727-1.4136262019-03-03-0.537312-0.658212-0.6218130.1122022019-03-04-0.4124361.1260502.175933-0.5678392019-03-05-0.4323810.7975430.980092-1.8547862019-03-060.1511900.7557860.768972-0.286186
data.sort_index(axis=1,ascending=True)
Out[30]:
A B C D
2019-03-011.866219-1.0030572.526899-0.5633432019-03-02-1.4136261.116727-0.470054-0.5679302019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.4124362019-03-05-1.8547860.9800920.797543-0.4323812019-03-06-0.2861860.7689720.7557860.151190
data.sort_values(by='A')
Out[31]:
A B C D
2019-03-05-1.8547860.9800920.797543-0.4323812019-03-02-1.4136261.116727-0.470054-0.5679302019-03-04-0.5678392.1759331.126050-0.4124362019-03-06-0.2861860.7689720.7557860.1511902019-03-030.112202-0.621813-0.658212-0.5373122019-03-011.866219-1.0030572.526899-0.563343
data.A
Out[32]:2019-03-011.8662192019-03-02-1.4136262019-03-030.1122022019-03-04-0.5678392019-03-05-1.8547862019-03-06-0.286186
Freq: D, Name: A, dtype: float64
data[2:4]
Out[33]:
A B C D
2019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.412436
data['20190302':'20190305']
Out[34]:
A B C D
2019-03-02-1.4136261.116727-0.470054-0.5679302019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.412436
loc效率较高
data.loc['20190302':'20190304']
Out[35]:
A B C D
2019-03-02-1.4136261.116727-0.470054-0.5679302019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.412436
data.iloc[2:4]
Out[36]:
A B C D
2019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.412436
data.loc[:,['B','C']]
Out[37]:
B C
2019-03-01-1.0030572.5268992019-03-021.116727-0.4700542019-03-03-0.621813-0.6582122019-03-042.1759331.1260502019-03-050.9800920.7975432019-03-060.7689720.755786
at效率更高访问特定值
data.at[pd.Timestamp('20190302'),'B']
Out[38]:1.116726953479249
data.iat[1,1]
Out[39]:1.116726953479249
布尔索引
data[data.A>0]
Out[40]:
A B C D
2019-03-011.866219-1.0030572.526899-0.5633432019-03-030.112202-0.621813-0.658212-0.537312
data[data>0]
Out[41]:
A B C D
2019-03-011.866219 NaN 2.526899 NaN
2019-03-02 NaN 1.116727 NaN NaN
2019-03-030.112202 NaN NaN NaN
2019-03-04 NaN 2.1759331.126050 NaN
2019-03-05 NaN 0.9800920.797543 NaN
2019-03-06 NaN 0.7689720.7557860.15119
data2=data.copy()
data2
Out[43]:
A B C D
2019-03-011.866219-1.0030572.526899-0.5633432019-03-02-1.4136261.116727-0.470054-0.5679302019-03-030.112202-0.621813-0.658212-0.5373122019-03-04-0.5678392.1759331.126050-0.4124362019-03-05-1.8547860.9800920.797543-0.4323812019-03-06-0.2861860.7689720.7557860.151190
tag=['a']*2+['b']*2+['c']*2
data2['TAG']=tag
data2
Out[46]:
A B C D TAG
2019-03-011.866219-1.0030572.526899-0.563343 a
2019-03-02-1.4136261.116727-0.470054-0.567930 a
2019-03-030.112202-0.621813-0.658212-0.537312 b
2019-03-04-0.5678392.1759331.126050-0.412436 b
2019-03-05-1.8547860.9800920.797543-0.432381 c
2019