1. 选取Series数据

  1. # 读取college数据集,查看CITY的前5行
  2. In[2]: college = pd.read_csv('data/college.csv', index_col='INSTNM')
  3. city = college['CITY']
  4. city.head()
  5. Out[2]: INSTNM
  6. Alabama A & M University Normal
  7. University of Alabama at Birmingham Birmingham
  8. Amridge University Montgomery
  9. University of Alabama in Huntsville Huntsville
  10. Alabama State University Montgomery
  11. Name: CITY, dtype: object
  1. # iloc可以通过整数选取
  2. In[3]: city.iloc[3]
  3. Out[3]: 'Huntsville'
  1. # iloc通过整数列表选取多行,返回结果是Series
  2. In[4]: city.iloc[[10,20,30]]
  3. Out[4]: INSTNM
  4. Birmingham Southern College Birmingham
  5. George C Wallace State Community College-Hanceville Hanceville
  6. Judson College Marion
  7. Name: CITY, dtype: object
  1. # 选择等分的数据,可以使用切片语法
  2. In[5]: city.iloc[4:50:10]
  3. Out[5]: INSTNM
  4. Alabama State University Montgomery
  5. Enterprise State Community College Enterprise
  6. Heritage Christian University Florence
  7. Marion Military Institute Marion
  8. Reid State Technical College Evergreen
  9. Name: CITY, dtype: object
  1. # loc只接收行索引标签
  2. In[6]: city.loc['Heritage Christian University']
  3. Out[6]: 'Florence'
  1. # 随机选择4个标签
  2. In[7]: np.random.seed(1)
  3. labels = list(np.random.choice(city.index, 4))
  4. labels
  5. Out[7]: ['Northwest HVAC/R Training Center',
  6. 'California State University-Dominguez Hills',
  7. 'Lower Columbia College',
  8. 'Southwest Acupuncture College-Boulder']
  1. # 通过标签列表选择多行
  2. In[8]: city.loc[labels]
  3. Out[8]: INSTNM
  4. Northwest HVAC/R Training Center Spokane
  5. California State University-Dominguez Hills Carson
  6. Lower Columbia College Longview
  7. Southwest Acupuncture College-Boulder Boulder
  8. Name: CITY, dtype: object
  1. # 也可以通过切片语法均匀选择多个
  2. In[9]: city.loc['Alabama State University':'Reid State Technical College':10]
  3. Out[9]: INSTNM
  4. Alabama State University Montgomery
  5. Enterprise State Community College Enterprise
  6. Heritage Christian University Florence
  7. Marion Military Institute Marion
  8. Reid State Technical College Evergreen
  9. Name: CITY, dtype: object
  1. # 也可以不使用loc,直接使用类似Python的语法
  2. In[10]: city['Alabama State University':'Reid State Technical College':10]
  3. Out[10]: INSTNM
  4. Alabama State University Montgomery
  5. Enterprise State Community College Enterprise
  6. Heritage Christian University Florence
  7. Marion Military Institute Marion
  8. Reid State Technical College Evergreen
  9. Name: CITY, dtype: object

更多

  1. # 要想只选取一项,并保留其Series类型,则传入一个只包含一项的列表
  2. In[11]: city.iloc[[3]]
  3. Out[11]: INSTNM
  4. University of Alabama in Huntsville Huntsville
  5. Name: CITY, dtype: object
  1. # 使用loc切片时要注意,如果start索引再stop索引之后,则会返回空,并且不会报警
  2. In[12]: city.loc['Reid State Technical College':'Alabama State University':10]
  3. Out[12]: Series([], Name: CITY, dtype: object)
  1. # 也可以切片逆序选取
  2. In[13]: city.loc['Reid State Technical College':'Alabama State University':-10]
  3. Out[13]: INSTNM
  4. Reid State Technical College Evergreen
  5. Marion Military Institute Marion
  6. Heritage Christian University Florence
  7. Enterprise State Community College Enterprise
  8. Alabama State University Montgomery
  9. Name: CITY, dtype: object