Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Python Pandas: Get index of rows which column matches certain value

Python Pandas: Get index of rows which column matches certain value

August 1, 2021 by James Palmer

df.iloc[i] returns the ith row of df. i does not refer to the index label, i is a 0-based index.
In contrast, the attribute index returns actual index labels, not numeric row-indices:
df.index[df[‘BoolCol’] == True].tolist()

or equivalently,
df.index[df[‘BoolCol’]].tolist()

You can see the difference quite clearly by playing with a DataFrame with
a non-default index that does not equal to the row’s numerical position:
df = pd.DataFrame({‘BoolCol’: [True, False, False, True, True]},
index=[10,20,30,40,50])

In [53]: df
Out[53]:
BoolCol
10 True
20 False
30 False
40 True
50 True

[5 rows x 1 columns]

In [54]: df.index[df[‘BoolCol’]].tolist()
Out[54]: [10, 40, 50]

If you want to use the index,
In [56]: idx = df.index[df[‘BoolCol’]]

In [57]: idx
Out[57]: Int64Index([10, 40, 50], dtype=’int64′)

then you can select the rows using loc instead of iloc:
In [58]: df.loc[idx]
Out[58]:
BoolCol
10 True
40 True
50 True

[3 rows x 1 columns]

Note that loc can also accept boolean arrays:
In [55]: df.loc[df[‘BoolCol’]]
Out[55]:
BoolCol
10 True
40 True
50 True

[3 rows x 1 columns]

If you have a boolean array, mask, and need ordinal index values, you can compute them using np.flatnonzero:
In [110]: np.flatnonzero(df[‘BoolCol’])
Out[112]: array([0, 3, 4])

Use df.iloc to select rows by ordinal index:
In [113]: df.iloc[np.flatnonzero(df[‘BoolCol’])]
Out[113]:
BoolCol
10 True
40 True
50 True

Can be done using numpy where() function:
import pandas as pd
import numpy as np

In [716]: df = pd.DataFrame({“gene_name”: [‘SLC45A1’, ‘NECAP2’, ‘CLIC4’, ‘ADC’, ‘AGBL4′] , “BoolCol”: [False, True, False, True, True] },
index=list(“abcde”))

In [717]: df
Out[717]:
BoolCol gene_name
a False SLC45A1
b True NECAP2
c False CLIC4
d True ADC
e True AGBL4

In [718]: np.where(df[“BoolCol”] == True)
Out[718]: (array([1, 3, 4]),)

In [719]: select_indices = list(np.where(df[“BoolCol”] == True)[0])

In [720]: df.iloc[select_indices]
Out[720]:
BoolCol gene_name
b True NECAP2
d True ADC
e True AGBL4

Though you don’t always need index for a match, but incase if you need:
In [796]: df.iloc[select_indices].index
Out[796]: Index([u’b’, u’d’, u’e’], dtype=’object’)

In [797]: df.iloc[select_indices].index.tolist()
Out[797]: [‘b’, ‘d’, ‘e’]

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in