简体   繁体   English

从Pandas Dataframe列返回最大和最小值的实际索引值

[英]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 . 我想返回count为1的所有索引 Furthermore movies_per_year.max() returns 5 . 而且movies_per_year.max()返回5 So, it should return 1898 . 因此,它应该返回1898

np.where() - returns positional indices of the elements that satisfy the given condition: np.where() -返回满足给定条件的元素的位置索引:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM