說我有從示例數據集在這里:import pandas as pdraw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], 'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], 'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], 'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3], 'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])df我想做一個regimentvs的箱線圖preTestScore。為此,我需要找出這兩個變量的相對分布。所以,我regiment按preTestScore以下方式分組:df1 = df['regiment'].groupby(df['preTestScore']).count()df1preTestScore2 33 34 224 231 2Name: regiment, dtype: int64
1 回答

幕布斯7119047
TA貢獻1794條經驗 獲得超8個贊
使用to_frame
該系列轉換成數據幀,然后繪制之前重置索引:
df1 = df['regiment'].groupby(df['preTestScore']).count().to_frame().reset_index() sns.boxplot(x='regiment', y='preTestScore', data=df1)
添加回答
舉報
0/150
提交
取消