繁体   English   中英

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

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

如何打印/返回特定值的索引?

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

在这里,我的索引是年份 我想返回count为1的所有索引 而且movies_per_year.max()返回5 因此,它应该返回1898

np.where() -返回满足给定条件的元素的位置索引:

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

要么

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

如果您需要实际索引值(标签)而不是它们的位置:

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

查找最大元素的索引:

In [32]: df['count'].idxmax()
Out[32]: 1898

暂无
暂无

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

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