Lets say I have this data
import pandas as pd
data = {
'country':['USA', 'China', 'Japan', 'Germany', 'UK', 'India', 'USA', 'India']
,'foo':[1, 2, 3, 4, 5, 6, 7, 8]
,'bar':[11, 22, 33, 44, 55, 66, 77, 88]
}
df = pd.DataFrame(data, columns = ['country', 'GDP', 'population'])
With df.groupby('country').agg(['count'])
I get the count()
, groupby
the country
.
GDP population
count count
country
China 1 1
Germany 1 1
India 2 2
Japan 1 1
UK 1 1
USA 2 2
How would I select the row
with the max()
value in the column
bar
or in the column
foo
? How to get the two max()
(here India and USA) values back?
CodePudding user response:
Try:
# find the rows with a maximum either in foo or bar
mask = (counts == counts.values.max(0)).any(1)
res = counts[mask]
print(res)
Output
foo bar
count count
country
India 2 2
USA 2 2