根据最大值提取值 - pandas 数据框
Extract value based on max value - pandas dataframe
我正在尝试根据另一列中的最大值 ('total_cases[=32) 提取值 ('location') =]') 并将其分配给 highest_no_new
我想从中提取值的先前数据框称为 no_new_cases
我想从[=13中挑选出'total_cases'数量最多的'location' =]
我可以访问最多的“total_cases”,但不能访问位置:
highest_no_new = no_new_cases['total_cases'].max()
请你帮忙 return 'location'?
提前致谢。
使用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
我正在尝试根据另一列中的最大值 ('total_cases[=32) 提取值 ('location') =]') 并将其分配给 highest_no_new
我想从中提取值的先前数据框称为 no_new_cases
我想从[=13中挑选出'total_cases'数量最多的'location' =]
我可以访问最多的“total_cases”,但不能访问位置:
highest_no_new = no_new_cases['total_cases'].max()
请你帮忙 return 'location'?
提前致谢。
使用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