2017-06-29 5 views
1

に各グループ化するための関数を定義したユーザ、次のように私は、データフレームDF1を持って適用することができます。どのように私はPythonの

Country|Month|Revenue 
-------|-----|------- 
US  |Jan |100 
US  |Feb |200 
US  |Mar |300 
Canada |Jan |200 
Canada |Feb |400 
Canada |Mar |500 

私は次のようにユーザー定義関数を適用したい:

df3=df1.groupby(['Country'])['Revenue'].my_cool_func() 
def my_cool_func(): 
    b = max(Revenue)-Min(Revenue) 
    c=b/2 
    return c 

DF3のための私の最終的な出力は次のようになります。

Country|my_cool_func_rev 
-------|---------------- 
US  |100 
Canada |150 

はどのようにして、出力の上に取得するユーザー定義関数を使用できますか?

答えて

0

あなたはGroupBy.applyを使用し、機能にSeriesでの作業なので、可能な用途は、Series.maxSeries.minであることができます。

def my_cool_func(x): 
    #print (x) 
    return (x.max() - x.min())/2 

df3=df1.groupby(['Country'])['Revenue'].apply(my_cool_func).reset_index() 
print (df3) 
    Country Revenue 
0 Canada 150.0 
1  US 100.0 

または:

df3=df1.groupby(['Country'])['Revenue'].apply(lambda x:(x.max() - x.min())/2).reset_index() 
print (df3) 
    Country Revenue 
0 Canada 150.0 
1  US 100.0 

EDIT:使用Series.std

def my_cool_func(x): 
    b = x.std() 
    c=b/2 
    return c 

df3=df1.groupby(['Country'])['Revenue'].apply(my_cool_func).reset_index() 
print (df3) 
    Country Revenue 
0 Canada 76.376262 
1  US 50.000000 
+0

私はどんな茶をしたくない標準偏差を持つ可能性がありますmaxとminの代わりに私のユーザー定義関数にges。その場合、どのようにして最初のオプションを使用できますか? –

+0

編集した回答を確認してください。 – jezrael

+0

私の答えが役に立ちましたら、[accept](http://meta.stackexchange.com/a/5235/295067)を忘れないでください。答えの横にあるチェックマーク( '✓')をクリックして、灰色で塗りつぶされた。ありがとう。 – jezrael

関連する問題