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