假設我有這個資料
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)
隨著df.groupby('country').agg(['count'])我得到的count(),groupby對country。
GDP population
count count
country
China 1 1
Germany 1 1
India 2 2
Japan 1 1
UK 1 1
USA 2 2
我怎么會選擇row與max()中值column bar或在column foo?如何取回這兩個max()(這里是印度和美國)的值?
uj5u.com熱心網友回復:
嘗試:
# find the rows with a maximum either in foo or bar
mask = (counts == counts.values.max(0)).any(1)
res = counts[mask]
print(res)
輸出
foo bar
count count
country
India 2 2
USA 2 2
正如@Stophface,而不是建議.values,使用to_numpy所推薦的大熊貓檔案。
mask = (counts == counts.to_numpy().max(0)).any(1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325902.html
