新しい DataFrame を手動で構築するだけかもしれません。あなたが持っている場合
>>> raw_data
user_id business_id data
0 10 1 5
1 20 10 6
2 20 100 7
3 30 100 8
次に、の結果はgroupby(smth).size().describe()
別のものSeries
です:
>>> raw_data.groupby("user_id").size().describe()
count 3.000000
mean 1.333333
std 0.577350
min 1.000000
25% 1.000000
50% 1.000000
75% 1.500000
max 2.000000
dtype: float64
>>> type(_)
<class 'pandas.core.series.Series'>
など:
>>> descrs = ((col, raw_data.groupby(col).size().describe()) for col in raw_data)
>>> pd.DataFrame.from_items(descrs).T
count mean std min 25% 50% 75% max
user_id 3 1.333333 0.57735 1 1 1 1.5 2
business_id 3 1.333333 0.57735 1 1 1 1.5 2
data 4 1.000000 0.00000 1 1 1 1.0 1
の代わりにfrom_items
、辞書を渡すこともできました
pd.DataFrame({col: raw_data.groupby(col).size().describe() for col in raw_data}).T
が、この方法では、列の順序を考えなくても保持されます。
すべての列が必要ない場合は、 の代わりにfor col in raw_data
、 を定義columns_to_describe = ["user_id", "business_id"] etc
して使用するfor col in columns_to_describe
か、を使用するfor col in raw_data if col.endswith("_id")
か、好きなものを使用できます。