14-numpy笔记-莫烦pandas-2

代码

import pandas as pd
import numpy as np

dates = pd.date_range('20130101', periods=6)
df=pd.DataFrame(np.arange(24).reshape((6,4)), index=dates, columns=['A','B','C','D'])
print('-1-')
print(df)
print('-2-')
print(df['A'],df.A)
print('-3-')
print(df[0:3],df['20130102':'20130104'])
print('-4-')
print(df.loc['20130102'])

print('-5-')
# 打印AB列
print(df.loc[:,['A','B']])

print('-6-')
print(df.loc['20130102',['A','B']])

# 筛选 [行,列] : 左闭右开
# 第三行,零初始
print('-7-')
print(df.iloc[3])
print('-8-')
print(df.iloc[3,1])
print('-9-')
print(df.iloc[3:5,1:3])
print('-10-')
print(df.iloc[[1,3,5],1:3])

print('-11-')
print(df.ix[:3,['A','C']])

print('-12-')
#条件筛选
print(df[df.A > 8])

  

结果

-1-
             A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
-2-
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int32 2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int32
-3-
            A  B   C   D
2013-01-01  0  1   2   3
2013-01-02  4  5   6   7
2013-01-03  8  9  10  11              A   B   C   D
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
-4-
A    4
B    5
C    6
D    7
Name: 2013-01-02 00:00:00, dtype: int32
-5-
             A   B
2013-01-01   0   1
2013-01-02   4   5
2013-01-03   8   9
2013-01-04  12  13
2013-01-05  16  17
2013-01-06  20  21
-6-
A    4
B    5
Name: 2013-01-02 00:00:00, dtype: int32
-7-
A    12
B    13
C    14
D    15
Name: 2013-01-04 00:00:00, dtype: int32
-8-
13
-9-
             B   C
2013-01-04  13  14
2013-01-05  17  18
-10-
             B   C
2013-01-02   5   6
2013-01-04  13  14
2013-01-06  21  22
-11-
            A   C
2013-01-01  0   2
2013-01-02  4   6
2013-01-03  8  10
-12-
             A   B   C   D
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23

  

 

posted @ 2018-09-07 10:34  路边的十元钱硬币  阅读(145)  评论(0编辑  收藏  举报