假設(shè)我有不同大小的數(shù)據(jù)集,例如X_1 = [1,2,3]和X_2 = [4,5,6,7,8]。我想創(chuàng)建一個包含匯總變量(平均值、標(biāo)準(zhǔn)差等)的數(shù)據(jù)框,每行一個數(shù)據(jù)集,每列一個統(tǒng)計數(shù)據(jù)。我怎么能在熊貓中做到這一點?
2 回答

斯蒂芬大帝
TA貢獻1827條經(jīng)驗 獲得超8個贊
我會用 describe
df=pd.concat([pd.Series(x) for x in [X_1, X_2]], axis=0, keys=['X_1', 'X_2'])# notice here I am using axis=0 rather than 1
df.groupby(level=0).describe()
Out[442]:
count mean std min 25% 50% 75% max
X_1 3.0 2.0 1.000000 1.0 1.5 2.0 2.5 3.0
X_2 5.0 6.0 1.581139 4.0 5.0 6.0 7.0 8.0
特別案例
X_1 = [1,2,np.nan]
X_2 = [4,5,6,7,8]
df=pd.concat([pd.Series(x) for x in [X_1, X_2]], axis=0, keys=['X_1', 'X_2'])
df.groupby(level=0).size()
Out[445]:
X_1 3
X_2 5
dtype: int64
添加回答
舉報
0/150
提交
取消