[英]Extract value based on max value - pandas dataframe
我正在尝试根据另一列(“ total_cases ”)中的最大值提取一个值(“ location ”)并将其分配给highest_no_new
我以前想从中提取值的数据no_new_cases
称为no_new_cases
我想挑选出与“total_cases”从最多的“位置” no_new_cases
我能够访问最多的“ total_cases ”,但不能访问位置:
highest_no_new = no_new_cases['total_cases'].max()
请你能帮忙返回“位置”吗?
提前致谢。
使用idxmax
:
df.loc[df['total_cases'].idxmax(), 'location']
您可以使用iloc
方法:
df['highest_no_new'] = df.iloc[df.total_cases.argmax()]['location']
示例:
>>> d = {'location': [1, 2], 'total_cases': [3, 4]}
>>> df = pd.DataFrame(d)
>>> df
location total_cases
0 1 3
1 2 4
>>> df.iloc[df.total_cases.argmax()]['location']
2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.