Pandas实战--人口收入普查数据探索

Pandas实战–人口收入普查数据探索

该习题来自实验楼,本篇文章记录自己在实验中的回答
习题直达链接

首先,我们加载并预览该数据集。

import warnings
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
warnings.filterwarnings('ignore')
data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
data.head()

在这里插入图片描述
DataFrame 前面的列均为特征,最后的 salary 为目标值。接下来,你需要自行补充必要的代码来回答相应的挑战问题。

问题1:数据集中有多少男性和女性?

 pd.value_counts(data.sex)

Male 21790
Female 10771
Name: sex, dtype: int64

问题2:数据集中女性的平均年龄是多少?

data[data['sex'] == 'Female']['age'].mean()

36.85823043357163

问题3:数据集中德国公民的比例是多少?

data[data['native-country'] == 'Germany'].shape[0]/data.shape[0]

0.004207487485028101

问题4:年收入超过 50K 和低于 50K 人群年龄的平均值和标准差是多少?

richer = data[data['salary'] == '>50K']['age']
poorer = data[data['salary'] == '<=50K']['age']
print('种类\t平均值\t标准差\t')
print('>50K\t'+str(richer.mean())+'\t'+str(richer.std())+'\t')
print('<=50K\t'+str(poorer.mean())+'\t'+str(poorer.std())+'\t')

种类 平均值 标准差

50K 44.24984058155847 10.51902771985177
<=50K 36.78373786407767 14.020088490824813

问题5:年收入超过 50K 的人群是否都接受过高中以上教育?

data[data['salary'] == '>50K']['education'].unique()
# 显示收入高于50K的人群中学历种类的集合,所以结果为NO

array([‘HS-grad’, ‘Masters’, ‘Bachelors’, ‘Some-college’, ‘Assoc-voc’,
‘Doctorate’, ‘Prof-school’, ‘Assoc-acdm’, ‘7th-8th’, ‘12th’,
‘10th’, ‘11th’, ‘9th’, ‘5th-6th’, ‘1st-4th’], dtype=object)

问题6:使用 groupby 和 describe 统计不同种族和性别人群的年龄分布数据。

data.groupby(['race','sex'])['age'].describe()

count mean std min 25% 50% 75% max
race sex
Amer-Indian-Eskimo Female 119.0 37.117647 13.114991 17.0 27.0 36.0 46.00 80.0
Male 192.0 37.208333 12.049563 17.0 28.0 35.0 45.00 82.0
Asian-Pac-Islander Female 346.0 35.089595 12.300845 17.0 25.0 33.0 43.75 75.0
Male 693.0 39.073593 12.883944 18.0 29.0 37.0 46.00 90.0
Black Female 1555.0 37.854019 12.637197 17.0 28.0 37.0 46.00 90.0
Male 1569.0 37.682600 12.882612 17.0 27.0 36.0 46.00 90.0
Other Female 109.0 31.678899 11.631599 17.0 23.0 29.0 39.00 74.0
Male 162.0 34.654321 11.355531 17.0 26.0 32.0 42.00 77.0
White Female 8642.0 36.811618 14.329093 17.0 25.0 35.0 46.00 90.0
Male 19174.0 39.652498 13.436029 17.0 29.0 38.0 49.00 90.0

问题7:统计男性高收入人群中已婚和未婚(包含离婚和分居)人群各自所占数量。

data[(data['sex'] == 'Male')&(data['salary'] == '>50K') & data['marital-status'].str.startswith('Married')].shape[0]

5965

data[(data['salary'] == '>50K')&(data['sex'] == 'Male') &
     (data['marital-status'].isin(['Never-married',
                                   'Separated', 'Divorced']))].shape[0]

658

问题8:统计数据集中最长周工作小时数及对应的人数,并计算该群体中收入超过 50K 的比例。

maxWorkHour = data['hours-per-week'].max()
#最长周工作小时数及对应的人数
m1 = data[data['hours-per-week'] == maxWorkHour]
ratio = m1[m1['salary'] == '>50K'].shape[0]/m1.shape[0]
print('最长的工作小时数为{0},对应的人数为{1},\n该群体中收入超过50K的比例为{2}'.format(maxWorkHour,m1.shape[0],ratio))

最长的工作小时数为99,对应的人数为85,
该群体中收入超过50K的比例为0.29411764705882354

问题9:计算各国超过和低于 50K 人群各自的平均周工作时长。

data.groupby(['native-country','salary'])['hours-per-week'].mean()

native-country salary
? <=50K 40.164760
>50K 45.547945
Cambodia <=50K 41.416667
>50K 40.000000
Canada <=50K 37.914634

United-States >50K 45.505369
Vietnam <=50K 37.193548
>50K 39.200000
Yugoslavia <=50K 41.600000
>50K 49.500000
Name: hours-per-week, Length: 82, dtype: float64
Markdown Code

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章