2017-01-15 8 views
3

を行った後、私はTTMという名前の、次のデータフレームがあります。、GROUPBYグループ化されたカラムは

usersidid clienthostid eventSumTotal LoginDaysSum score 
0  12   1    60    3   1728 
1  11   1    240    3   1331 
3  5   1    5    3   125 
4  6   1    16    2   216 
2  10   3    270    3   1000 
5  8   3    18    2   512 

私は

ttm.groupby(['clienthostid'], as_index=False, sort=False)['LoginDaysSum'].count() 

を行うと、私は思っていたでしょうが、私は(私が期待したものを取得します「比」という名前の新しいラベルの下にあるとの結果):

 clienthostid LoginDaysSum 
0    1   4 
1    3   2 

しかし、私は

を行います
ttm.groupby(['clienthostid'], as_index=False, sort=False)['LoginDaysSum'].apply(lambda x: x.iloc[0]/x.iloc[1]) 

私が取得:

0 1.0 
1 1.5 
  1. なぜラベルは行きましたか?私はまだ、「clienthostid」というグループ化されたニーズが必要です。また、ラベルの下にも適用結果が必要です
  2. 時々私がgroupbyを実行すると、他の列のいくつかが表示されることがあります。いつか滞在しますか?私はそれらのことを行う行方不明のフラグがありますか?
  3. 私が与えた例では、結果を数えたとき、ラベル 'LoginDaysSum'に結果が表示されましたが、その代わりに結果の新しいラベルを追加する理由はありますか?

groupbyは2つの可能な解決策になった後の戻りDataFrameについて

答えて

5

、ありがとう:countで素敵な作品何

  1. パラメータas_index=Falsesummean機能

  2. reset_indexのための作成レベルの新しい列第二の必要性については、より一般的な解決策

df = ttm.groupby(['clienthostid'], as_index=False, sort=False)['LoginDaysSum'].count() 
print (df) 
    clienthostid LoginDaysSum 
0    1    4 
1    3    2 
df = ttm.groupby(['clienthostid'], sort=False)['LoginDaysSum'].count().reset_index() 
print (df) 
    clienthostid LoginDaysSum 
0    1    4 
1    3    2 

as_index=Falseを削除し、代わりにreset_indexを追加します。

#output is `Series` 
a = ttm.groupby(['clienthostid'], sort=False)['LoginDaysSum'] \ 
     .apply(lambda x: x.iloc[0]/x.iloc[1]) 
print (a) 
clienthostid 
1 1.0 
3 1.5 
Name: LoginDaysSum, dtype: float64 

print (type(a)) 
<class 'pandas.core.series.Series'> 

print (a.index) 
Int64Index([1, 3], dtype='int64', name='clienthostid') 


df1 = ttm.groupby(['clienthostid'], sort=False)['LoginDaysSum'] 
     .apply(lambda x: x.iloc[0]/x.iloc[1]).reset_index(name='ratio') 
print (df1) 
    clienthostid ratio 
0    1 1.0 
1    3 1.5 

一部の列がなくなっているのはなぜ?

私は問題automatic exclusion of nuisance columnsがあることができると思う:

#convert column to str 
ttm.usersidid = ttm.usersidid.astype(str) + 'aa' 
print (ttm) 
    usersidid clienthostid eventSumTotal LoginDaysSum score 
0  12aa    1    60    3 1728 
1  11aa    1   240    3 1331 
3  5aa    1    5    3 125 
4  6aa    1    16    2 216 
2  10aa    3   270    3 1000 
5  8aa    3    18    2 512 

#removed str column userid 
a = ttm.groupby(['clienthostid'], sort=False).sum() 
print (a) 
       eventSumTotal LoginDaysSum score 
clienthostid          
1      321   11 3400 
3      288    5 1512 

What is the difference between size and count in pandas?

+0

私はOPがバグを発見したと思います。 – chrisaycock

+0

@chrisaycock - それはバグではないようです。 – jezrael

+0

ニースの説明+1 – ade1e

3

countgroupbyオブジェクトとパンダのための方法で構築され、それをどうするかを知っています。アウト・プットがどのように見えるかを決定する他の2つのことが指定されています。

#       For a built in method, when 
#       you don't want the group column 
#       as the index, pandas keeps it in 
#       as a column. 
#        |----||||----| 
ttm.groupby(['clienthostid'], as_index=False, sort=False)['LoginDaysSum'].count() 

    clienthostid LoginDaysSum 
0    1    4 
1    3    2 

#       For a built in method, when 
#       you do want the group column 
#       as the index, then... 
#        |----||||---| 
ttm.groupby(['clienthostid'], as_index=True, sort=False)['LoginDaysSum'].count() 
#              |-----||||-----| 
#             the single brackets tells 
#             pandas to operate on a series 
#             in this case, count the series 

clienthostid 
1 4 
3 2 
Name: LoginDaysSum, dtype: int64 

ttm.groupby(['clienthostid'], as_index=True, sort=False)[['LoginDaysSum']].count() 
#              |------||||------| 
#            the double brackets tells pandas 
#            to operate on the dataframe 
#            specified by these columns and will 
#            return a dataframe 

       LoginDaysSum 
clienthostid    
1      4 
3      2 

あなたがapplyパンダを使用すると、もはやあなたがas_index=Falseを言うときに、グループの列に何をすべきかを知りません。 applyを使用した場合、あなたが返すと言ったものが正確に返されたかったので、それを放棄することを信頼する必要があります。また、一連の列を操作すると言う列の周りに単一の括弧があります。代わりに、as_index=Trueを使用して、索引にグループ化列情報を保持します。それから、reset_indexでフォローアップし、インデックスからデータフレームに戻します。この時点では、reset_indexの後にもう一度データフレームが作成されるため、単一の括弧を使用していることは重要ではありません。

ttm.groupby(['clienthostid'], as_index=True, sort=False)['LoginDaysSum'].apply(lambda x: x.iloc[0]/x.iloc[1]) 

0 1.0 
1 1.5 
dtype: float64 

ttm.groupby(['clienthostid'], as_index=True, sort=False)['LoginDaysSum'].apply(lambda x: x.iloc[0]/x.iloc[1]).reset_index() 

    clienthostid LoginDaysSum 
0    1   1.0 
1    3   1.5 
関連する問題