Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning the actual index value of max & min values from a Pandas Dataframe column

How do I print/return the index of a particular value?

movies_per_year = pd.DataFrame(movies_per_year)
movies_per_year.columns = ["count"]
print(movies_per_year)

        count
year       
1891      1
1893      1
1894      2
1895      2
1896      2
1898      5

Here, my index is the year. I want to return all the indexes where count is 1. Furthermore movies_per_year.max() returns 5. So, it should return 1898.

like image 391
Yash Ghorpade Avatar asked May 22 '18 09:05

Yash Ghorpade


People also ask

How do I get the index of the minimum and maximum values?

Sign in to answer this question. The "min" and "max" functions in MATLAB return the index of the minimum and maximum values, respectively, as an optional second output argument.

How to get the index of the max value in NumPy?

The following code shows how to get the index of the max value in a one-dimensional NumPy array: The argmax () function returns a value of 2. This tells us that the value in index position 2 of the array contains the maximum value.

How do I find the maximum value in a range?

To lookup information related to the maximum value in a range, you can use a formula that combines the MAX, INDEX, and MATCH functions. In the example shown, the formula in I8 is: which returns the number 3920, representing the square footage (Sq. Ft.) of the most expensive property in the list.

How to find the position of Max Value in list?

Position of max value in list. To get the position of the maximum value in a range (i.e. a list, table, or row), you can use the MAX function together with the MATCH function. In the example shown, the formula in I5 is: = MATCH ( MAX ( C3:C11 ), C3:C11 , 0 ) Which returns the...


1 Answers

np.where() - returns positional indices of the elements that satisfy the given condition:

In [31]: np.where(df['count'] == 1)[0]
Out[31]: array([0, 1], dtype=int64)

or

In [35]: np.nonzero(df['count'] == 1)
Out[35]: (array([0, 1], dtype=int64),)

if you need real index values (labels) instead of their positions:

In [40]: df.index[df['count'] == 1]
Out[40]: Int64Index([1891, 1893], dtype='int64', name='year')

to find an index of a maximum element:

In [32]: df['count'].idxmax()
Out[32]: 1898
like image 129
MaxU - stop WAR against UA Avatar answered Sep 30 '22 11:09

MaxU - stop WAR against UA