Reputation: 3253
Let us say we have the following table
and I want to find max and min value for every row for a set of specific columns (let's say CENSUS2010POP, ESTIMATESBASE1010, POPESTIMATE2010). How to do it with Pandas?
Upvotes: 8
Views: 10287
Reputation: 1179
Suppose you have the following data df
a b c
0.2 1.2 9
0.5 2.1 1.1
3.9 0.2 1.9
4.6 3.3 7.8
If you want is to find max value for each row BUT for only few columns, you could do column slicing (for example, columns a and b only) as follows :
df.assign(max_value=(df[df.columns[0:2]]).values.max(1))
The result will be:
a b c max_value
0 0.2 1.2 9.0 1.2
1 0.5 2.1 1.1 2.1
2 3.9 0.2 1.9 3.9
3 4.6 3.3 7.8 4.6
Upvotes: 0
Reputation: 862591
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
Upvotes: 8