随机抽样

使用 Python 分析调查数据

EbunOluwa Andrew

Data Scientist

调查数据分析中的抽样

  • 抽样=从总体中取小样本
    • 用于推断总体
    • 抽样使数据可管理
    • 抽样会产生抽样误差
    • 总体越大,误差越小

Photo by Patrick Fore on Unsplash - 孩子拿着糖果

使用 Python 分析调查数据

随机抽样

  • 每个成员被选中的机会相同
  • 降低偏差
  • 内部效度高
  • 外部效度高

彩票

使用 Python 分析调查数据

.sample() 方法

  • DataFrame.sample(n = _None_, frac = _None_, random_state = _None_)
  • n = 抽取的条目数
  • frac = 返回的比例(0–1)
  • random_state = 随机种子,用于可复现结果
使用 Python 分析调查数据

随机抽样示例

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample(n=100) print(sample)
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 3244  | fffe330     | Female | Yes         |
| 21339 | fffe310     | Male   | Yes         |
| 1122  | fffe390     | Male   | Yes         |
| 4363  | fffe313     | Female | Yes         |
使用 Python 分析调查数据

随机抽样示例

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample(frac = 0.1) print(sample)
|     | employee_id | gender | onsite_work |
|-----|-------------|--------|-------------|
| 142 | fffe800     | Female | Yes         |
| 710 | fffe900     | Female | Yes         |
| 242 | fffe700     | Female | Yes         |
| 114 | fffe600     | Female | Yes         |
使用 Python 分析调查数据

随机抽样示例

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample( n = 100, random_state = 123)
import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample( frac = 0.1, random_state = 123)
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 21383 | fffe3       | Female | Yes         |
| 82    | fffe0       | Male   | Yes         |
| 20739 | fffe2       | Male   | Yes         |
| 7662  | fffe9       | Female | Yes         |
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 21383 | fffe3       | Female | Yes         |
| 82    | fffe0       | Male   | Yes         |
| 20739 | fffe2       | Male   | Yes         |
| 7662  | fffe9       | Female | Yes         |
使用 Python 分析调查数据

Passons à la pratique !

使用 Python 分析调查数据

Preparing Video For Download...