[英]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.