I have the following DataFrame:
a b c b 2 1 2 3 5 4 5 6
As you can see, column b
is used as an index. I want to get the ordinal number of the row fulfilling ('b' == 5)
, which in this case would be 1
.
The column being tested can be either an index column (as with b
in this case) or a regular column, e.g. I may want to find the index of the row fulfilling ('c' == 6)
.
Using count() method in Python Pandas we can count the rows and columns. Count method requires axis information, axis=1 for column and axis=0 for row. To count the rows in Python Pandas type df. count(axis=1) , where df is the dataframe and axis=1 refers to column.
If you would like to find just the matched indices of the dataframe that satisfies the boolean condition passed as an argument, pandas. DataFrame. index() is the easiest way to achieve it. In the above snippet, the rows of column A matching the boolean condition == 1 is returned as output as shown below.
Often you may want to select the rows of a pandas DataFrame based on their index value. If you'd like to select rows based on integer indexing, you can use the . iloc function.
In the Pandas DataFrame we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter.
Use Index.get_loc instead.
Reusing @unutbu's set up code, you'll achieve the same results.
>>> import pandas as pd >>> import numpy as np >>> df = pd.DataFrame(np.arange(1,7).reshape(2,3), columns = list('abc'), index=pd.Series([2,5], name='b')) >>> df a b c b 2 1 2 3 5 4 5 6 >>> df.index.get_loc(5) 1
You could use np.where like this:
import pandas as pd import numpy as np df = pd.DataFrame(np.arange(1,7).reshape(2,3), columns = list('abc'), index=pd.Series([2,5], name='b')) print(df) # a b c # b # 2 1 2 3 # 5 4 5 6 print(np.where(df.index==5)[0]) # [1] print(np.where(df['c']==6)[0]) # [1]
The value returned is an array since there could be more than one row with a particular index or value in a column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With