简体   繁体   English

如何获得熊猫中出现最大值和最小值的年份

[英]How to get the year where the max and min value occurs in pandas

enter image description here在此处输入图片说明

Please take a look at the picture for df请看图为df

I have this set of data-frame.我有这组数据框。 I found the mean, max, and min value that occurred from 2008-2012 using:我使用以下方法找到了 2008-2012 年发生的平均值、最大值和最小值:

mean=(df.iloc[1:6,1].mean())
print(f"Mean for dividends from 2008-2012 is {mean}")
column=(df.iloc[1:6,1])
max_value=column.max()
min_value=column.min()

How am I supposed to get the year where the max and min occurred from 2008-2012?我应该如何获得 2008-2012 年出现最大值和最小值的年份?

You can get row number using argmin and argmax您可以使用 argmin 和 argmax 获取行号

min_value_row = column.argmin()
max_value_row = column.argmax()

Then you can use it to get date (expecting 0 column is the date):然后你可以用它来获取日期(期望 0 列是日期):

date_min = df.iloc[column.argmin(),0]
date_max = df.iloc[column.argmax(),0]

In your case:在你的情况下:

column_dividends_selected = df.Dividends.iloc[1:6]

min_value = column_dividends_selected.min()
max_value = column_dividends_selected.max()

min_value_row = column_dividends_selected.argmin()
max_value_row = column_dividends_selected.argmax()


column_date_selected = df.Year.iloc[1:6]

date_min = column_date_selected.iloc[min_value_row]
date_max = column_date_selected.iloc[max_value_row]

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

相关问题 如何对用户输入的两位或三位数字使用 max() 和 min() - How to use max() and min() for two or three digit numbers from user input Python。 Pandas。 获取一个值到达另一个值所需的时间(差异) - Python. Pandas. Get the time (difference) a value takes to get to another value 汇总列的每个唯一元素的最大和最小日期 - Summarizing max and min dates for each unique element of a column 查找 pandas datafrmae 的最小索引,其中列值相等 - Finding the smallest indices of a pandas datafrmae where column value equality holds 在pandas数据帧中的groupby之后查找最大行 - Finding max row after groupby in pandas dataframe Pandas 1.0 从年份和日期创建月份列 - Pandas 1.0 create column of months from year and date 如何保留 pandas 石斑鱼和分组依据中的日期时间列? - How to retain datetime column in pandas grouper and group by? 熊猫列比较,使两列同时不等于一个值 - Pandas column comparison so that two columns simultaneously are not equal to a value Pandas select 基于绝对值,每组前 2 名 - Pandas select top 2 from each group based on absolute value 我想在 python 中生成随机月、日、小时、分钟、秒,但在此代码中生成随机月不起作用。 我怎样才能做到这一点? - I want to generate random month, day, hour, min, sec in python but in this code generating random month is not working. How can I do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM