pandas之ix 、loc 和 iloc的区别

在Python中处理日常数据时,经常会需要索引某些行,现结合本人使用经验做一个小的总结,pandas中主要有以下函数:

 1、 loc

 2、iloc

 3、ix

首先来谈谈loc:

其是通过行标签索引行数据

import pandas as pd
df = pd.DataFrame({'team_A': ['Spain', 'Germany', 'Brazil', 'France'],
                  'team_B': ['USA', 'Argentina', 'Mexico', 'Belgium'],
                  'score_A': [5, 3, 2, 0],
                  'score_B': [4, 0, 3, 0]},
                 columns = ['team_A', 'team_B', 'score_A', 'score_B'])
df
Out[2]:
    team_A     team_B  score_A  score_B
0    Spain        USA        5        4
1  Germany  Argentina        3        0
2   Brazil     Mexico        2        3
3   France    Belgium        0        0

如果我们索引第三行,则可以通过如下操作:

#当索引为整数时
df.loc[2]
Out[3]: 
team_A     Brazil
team_B     Mexico
score_A         2
score_B         3
Name: 2, dtype: object

#当索引为字符时
df.loc['2']
结果类似

#索引多行
df.loc[:3]
Out[5]: 
    team_A     team_B  score_A  score_B
0    Spain        USA        5        4
1  Germany  Argentina        3        0
2   Brazil     Mexico        2        3
3   France    Belgium        0        0

#索引多行多列
df.loc[2,['score_A','score_B']]
Out[7]: 
score_A    2
score_B    3
Name: 2, dtype: object 

其次是iloc—通过行号获取行数据

如果想要获取哪一行就输入该行数字,如果通过行标签索引会报错:

#索引某一行,输入该行数字
df.iloc[2]
Out[10]: 
team_A     Brazil
team_B     Mexico
score_A         2
score_B         3
Name: 2, dtype: object

#索引多行
df.iloc[1:]
Out[11]: 
    team_A     team_B  score_A  score_B
1  Germany  Argentina        3        0
2   Brazil     Mexico        2        3
3   France    Belgium        0        0

#索引列
df.iloc[:,[2]]
Out[15]: 
   score_A
0        5
1        3
2        2
3        0

最后是ix—结合前两种的混合索引

ix是结合了前两种方法,既可以通过行标签也可以通过行数字进行索引,详细如下:


import pandas as pd
df = pd.DataFrame({'team_A': ['Spain', 'Germany', 'Brazil', 'France'],
                  'team_B': ['USA', 'Argentina', 'Mexico', 'Belgium'],
                  'score_A': [5, 3, 2, 0],
                  'score_B': [4, 0, 3, 0]},
                 columns = ['team_A', 'team_B', 'score_A', 'score_B'],
                 index=['one','two','three','four'])
#行号
df.ix[2]
Out[17]: 
team_A     Brazil
team_B     Mexico
score_A         2
score_B         3
Name: 2, dtype: object

#行标签
df.ix['three']
Out[26]: 
team_A     Brazil
team_B     Mexico
score_A         2
score_B         3
Name: three, dtype: object
终于在节前放假写完了这一篇~





猜你喜欢

转载自blog.csdn.net/weixin_37536446/article/details/80675843
今日推荐