python--数据选取loc/iloc/ix

loc中的数据是列名,是字符串,所以前后都要取;iloc中数据是int整型,所以是Python默认的前闭后开

一. loc函数:主要通过行标签索引数据,前闭后闭

df
      0   1     2       3
0  green   M  10.1  class1
1    red   L  13.5  class2
2   blue  XL  15.3  class1

In[10]: df.loc[0:1]  #取第一和第二行,loc[]中的数字其实是行索引,所以算是前闭加后闭
Out[10]: 
       0  1     2       3
0  green  M  10.1  class1
1    red  L  13.5  class2

二、iloc函数:主要通过行号(序号)获取数据,前闭后开

正确

data.loc[:,'month_release'],data.iloc[;,1:5]对

错误:loc是标签,而iloc必须是序号

data.iloc[:,'month_release']错

三、ix:结合以上两种

另,一些筛选操作

data=data[data['month_release']>='2019-01']

你可能感兴趣的:(Python)