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

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