新手試圖打破我對excel的沉迷。我有一個支付發票的數據集與供應商和支付的數額一起的國家。我想知道每個供應商,哪個國家他們有最大的發票金額和他們的總業務在該國的百分比。使用該數據集我想要得到的結果是:Extract row with max value和Getting max value using groupby在DataFrameGroupBy中提取最大值的行
2:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Company' : ['bar','foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo', 'bar'],
'Country' : ['two','one', 'one', 'two', 'three', 'two', 'two', 'one', 'three', 'one'],
'Amount' : [4, 2, 2, 6, 4, 5, 6, 7, 8, 9],
'Pct' : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]})
CoCntry = df.groupby(['Company', 'Country'])
CoCntry.aggregate(np.sum)
看多的例子包括後Python : Getting the Row which has the max value in groups using groupby我已經得到儘可能創造一個DataFrameGroupBy總結各國的發票數據。我正在努力尋找最大的排。之後,我必須弄清楚如何計算百分比。忠告歡迎。
我不知道爲什麼,但聲明「G = CoCntry.groupby(水平=‘公司’)‘金額’]」觸發錯誤,「ttributeError:無法訪問調用屬性「 'DataFrameGroupBy'對象的'groupby',請嘗試使用'apply'方法「 – jones5322
第二種解決方案效果很好。非常感謝。 – jones5322
@AlbertJones - 我不知道什麼是問題,也許需要升級熊貓 - 在0.19.2中它是完美的。 – jezrael