pandas 如何为所选列选择一行中的最大值和最小值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41439419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:42:11 来源:igfitidea点击:
How to select max and min value in a row for selected columns
提问by YohanRoth
采纳答案by jezrael
df_subset=df.set_index('CTYNAME')[['CENSUS2010POP', 'ESTIMATESBASE1010', 'POPESTIMATE2010']]
df1 = df_subset.min(axis=1)
print (df1)
df2= df_subset.max(axis=1)
print (df2)
EDIT:
编辑:
df = pd.DataFrame({'CTYNAME':['Alabama','Autauga County','Baldwin County','Barbour County'],
'CENSUS2010POP':[4,5,6,2],
'ESTIMATESBASE1010':[7,8,9,3],
'POPESTIMATE2010':[1,3,5,5]})
print (df)
CENSUS2010POP CTYNAME ESTIMATESBASE1010 POPESTIMATE2010
0 4 Alabama 7 1
1 5 Autauga County 8 3
2 6 Baldwin County 9 5
3 2 Barbour County
df_subset=df.set_index('CTYNAME')[['CENSUS2010POP', 'ESTIMATESBASE1010', 'POPESTIMATE2010']]
df1 = df_subset.max(axis=1) - df_subset.min(axis=1)
print (df1)
CTYNAME
Alabama 6
Autauga County 5
Baldwin County 4
Barbour County 3
dtype: int64
print (df1.nlargest(1).reset_index(name='top1'))
CTYNAME top1
0 Alabama 6