如何将对熊猫数据框进行分组操作后获得的数据结构转换为数据框?

说我有从示例数据集在这里:


import pandas as pd


raw_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

http://img1.mukewang.com/60ac9bb40001c39f04330345.jpg

我想做一个regimentvs的箱线图preTestScore。为此,我需要找出这两个变量的相对分布。所以,我regiment按preTestScore以下方式分组:


df1 = df['regiment'].groupby(df['preTestScore']).count()

df1


preTestScore

2     3

3     3

4     2

24    2

31    2

Name: regiment, dtype: int64


函数式编程
浏览 160回答 1
1回答

幕布斯7119047

使用to_frame该系列转换成数据帧,然后绘制之前重置索引:df1 = df['regiment'].groupby(df['preTestScore']).count().to_frame().reset_index() sns.boxplot(x='regiment', y='preTestScore', data=df1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python