本文说明如何利用Pandas获得符合条件的数据的行/列索引值。 Updated: 2022/3/2
Python | Pandas | 获取行/列索引
获取行索引
根据列标签和指定的列的值,获取符合条件的数据所在的行的行索引
filepath = '.\\2022-02-25'
data = pd.read_excel(filepath, sheet_name='data')
'''
data.columns
# Index(['logged', 'updated', 'head', 'project', 'Hrs'], dtype='object')
data.index
# RangeIndex(start=0, stop=58, step=1)
'''
head 列中值为High 的数据所在的行及head 列
headHighVal = data.loc[data['head']=='High', ['head']]
'''
headHighVal
# head
# 4 High
# 5 High
# 16 High
# 17 High
# 21 High
# 22 High
# 23 High
# 26 High
# 42 High
# 46 High
# 47 High
# 50 High
# 54 High
headHighVal.columns
# Index(['head'], dtype='object')
headHighVal.index
# Int64Index([4, 5, 16, 17, 21, 22, 23, 26, 42, 46, 47, 50, 54], dtype='int64')
'''
headHighVals = data.loc[data['head'] == 'High']
'''
headHighVals
# logged ... Hrs
# 4 2022-02-18T13:44:22.216+0800 ... 4.0
# 5 2022-02-18T13:45:11.866+0800 ... 16.0
# 16 2022-02-21T15:33:49.883+0800 ... 4.0
# 17 2022-02-23T15:41:28.061+0800 ... 4.0
# 21 2022-02-22T12:30:01.384+0800 ... 8.0
# 22 2022-02-23T17:26:46.224+0800 ... 8.0
# 23 2022-02-24T09:57:55.513+0800 ... 2.0
# 26 2022-02-22T12:13:29.029+0800 ... 16.0
# 42 2022-02-22T15:53:38.472+0800 ... 1.0
# 46 2022-02-18T17:17:38.403+0800 ... 20.0
# 47 2022-02-24T10:57:13.531+0800 ... 8.0
# 50 2022-02-24T15:58:30.605+0800 ... 1.0
# 54 2022-02-23T11:12:54.924+0800 ... 2.0
#
# [13 rows x 12 columns]
headHighVals.columns
# Index(['logged', 'updated', 'head', 'project', 'Hrs'], dtype='object')
headHighVals.index
# Int64Index([4, 5, 16, 17, 21, 22, 23, 26, 42, 46, 47, 50, 54], dtype='int64')
'''
head 列中值为High 且project 列中值为P 的数据所在的行及head 和project 列
headprj = data.loc[(data['head']=='High')&(data['project']=='P'),['head','project']]
'''
headprj
# head project
# 4 High P
# 5 High P
# 42 High P
# 46 High P
# 47 High P
# 50 High P
headprj.columns
# Index(['head', 'project'], dtype='object')
headprj.index
# Int64Index([4, 5, 42, 46, 47, 50], dtype='int64')
'''
参考链接
|