pandas 基础入门 DataFrame的索引


如何从数据框中检索出自己要的数据:

data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
            'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions',
                     'Lions', 'Lions'],
            'wins': [11, 8, 10, 15, 11, 6, 10, 4],
            'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
    football = pd.DataFrame(data)
    print football['year']
    print ''
    print football.year  # shorthand for football['year']
    print ''
    print football[['year', 'wins', 'losses']]

这里我们用两种方式检索了‘year’这列的数据,也可同时列出好几列的数据:
看结果:

0    2010
1    2011
2    2012
3    2011
4    2012
5    2010
6    2011
7    2012
Name: year, dtype: int64

0    2010
1    2011
2    2012
3    2011
4    2012
5    2010
6    2011
7    2012
Name: year, dtype: int64

   year  wins  losses
0  2010    11       5
1  2011     8       8
2  2012    10       6
3  2011    15       1
4  2012    11       5
5  2010     6      10
6  2011    10       6
7  2012     4      12

  • 获取某一行的数据:
 football.iloc[[x]],football.loc[[]x]
#获取第一行的
football.loc[[0]]
football.iloc[0]
  • 获取一个区间的行数:
 football[x:y]

比如说第2行到底5行的:

football[2:6]
  • 根据某个条件查询:
football[条件]

比如获取胜利场次大于10场的:

football[football.wins > 10]

获取‘Pachers’队胜利超过10场数据:

football[(football.wins>10) & (football.team == 'Packers')]

下面来跑下数据验证上面的方法:

import pandas as pd
data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
            'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions',
                     'Lions', 'Lions'],
            'wins': [11, 8, 10, 15, 11, 6, 10, 4],
            'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
    football = pd.DataFrame(data)
    print football.iloc[[0]]
    print "---------------------"
    print football.loc[[0]]
    print "----------------------"
    print football[3:5]
    print "----------------------"
    print football[football.wins > 10]
    print "----------------------"
    print football[(football.wins > 10) & (football.team == "Packers")]

查看下运行结果:

   losses   team  wins  year
0       5  Bears    11  2010
--------------------------------------
   losses   team  wins  year
0       5  Bears    11  2010
-----------------------------------
   losses     team  wins  year
3       1  Packers    15  2011
4       5  Packers    11  2012
-------------------------------------
   losses     team  wins  year
0       5    Bears    11  2010
3       1  Packers    15  2011
4       5  Packers    11  2012
-------------------------------------
   losses     team  wins  year
3       1  Packers    15  2011
4       5  Packers    11  2012

Perfect

你可能感兴趣的:(pandas 基础入门 DataFrame的索引)