科技工作者心理健康数据分析
数据
统计各国 男性、女性心理健康数据分布
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by xuehz on 2017/2/19
'''
目标:数据集 性别的统计
'''
import csv
data_path = '/Users/xuehaozhe/python--Kaggle/升级版/1/codes/lect01_proj/survey.csv'
def run_main():
'''
返回形式:如 {'United States': [20, 50], 'Canada': [30, 40]}
其中每个元素是键值对,“键”是国家名称,“值”是列表结构,
列表的第一个数为该国家女性统计数据,第二个数为该国家男性统计数据
:return: result_dict = {}
'''
male_set = {'male','m'} #男性的可能取值
female_set = {'female', 'f'} # “女性”可能的取值
result_dict = {}
with open(data_path, 'r', newline='') as csvfile:
rows = csv.reader(csvfile)
for i ,row in enumerate(rows):
if i == 0:
continue
if i % 50 == 0:
print('正在处理第{}行数据。。。'.format(i))
# print(row) ['2014-08-27 11:44:43', '30', 'male', 'United States', 'IL', 'No', 'Yes',
# break
gender_val = row[2]
country_val = row[3]
## 数据清洗
gender_val = gender_val.replace(' ', '')
# 转换为小写
gender_val = gender_val.lower()
# 判断“国家”是否已经存在
if country_val not in result_dict:
# 如果不存在,初始化数据
result_dict[country_val] = [0,0]
if gender_val in female_set:
#女性
result_dict[country_val][0] += 1
elif gender_val in male_set:
# 男性
result_dict[country_val][1] += 1
else:
# 噪声数据,不做处理
pass
# 结果写入csv
with open('gender_country.csv','w',newline='', encoding='utf-16' ) as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',')
# 写入表头
csvwriter.writerow(['国家', '男性', '女性'])
for k, v in list(result_dict.items()):
csvwriter.writerow([k, v[0], v[1]])
if __name__ == '__main__':
run_main()
结果
统计各个国家 存在心理健康的平均年龄
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by xuehz on 2017/2/21
'''
目标:统计各个国家 存在心理健康的平均年龄
'''
import csv
data_path = '/Users/xuehaozhe/python--Kaggle/升级版/1/codes/lect01_proj/survey.csv'
def run_main():
"""
返回形式:如 {'United States': 34, 'Canada': 40}
:return: result_dict
"""
result_dict = {}
with open(data_path,'r',newline='') as csvfile:
rows = csv.reader(csvfile)
for i , row in enumerate(rows):
if i == 0:
continue
age_val = row[1]
country_val = row[3]
if (int(age_val) > 100) | (int(age_val) < 18):
#print("wrong age")
continue
if country_val not in result_dict:
result_dict[country_val] = [0,0]
result_dict[country_val][0] += int(age_val)
result_dict[country_val][1] += 1
for k,v in result_dict.items():
print('国家{}:平均年龄{}'.format(k, int(v[0]/v[1])))
if __name__ == '__main__':
run_main()
国家Hungary:平均年龄27
国家Slovenia:平均年龄19
国家Sweden:平均年龄26
国家China:平均年龄40
国家South Africa:平均年龄36
国家Poland:平均年龄30
国家Colombia:平均年龄27
国家Greece:平均年龄36
国家Czech Republic:平均年龄33
国家Bosnia and Herzegovina:平均年龄25
国家Ireland:平均年龄31
国家Belgium:平均年龄29
国家Denmark:平均年龄35
国家Russia:平均年龄24
国家Canada:平均年龄29
国家Uruguay:平均年龄26
国家United Kingdom:平均年龄30
国家Bulgaria:平均年龄28
国家Netherlands:平均年龄31
国家Austria:平均年龄26
国家Moldova:平均年龄26
国家Norway:平均年龄33
国家Brazil:平均年龄27
国家Singapore:平均年龄34
国家Romania:平均年龄29
国家Costa Rica:平均年龄38
国家United States:平均年龄33
国家Georgia:平均年龄20
国家India:平均年龄24
国家Nigeria:平均年龄25
国家Israel:平均年龄25
国家Spain:平均年龄30
国家Italy:平均年龄33
国家Japan:平均年龄49
国家France:平均年龄31
国家Mexico:平均年龄34
国家Portugal:平均年龄32
国家Finland:平均年龄29
国家Philippines:平均年龄31
国家Switzerland:平均年龄29
国家Latvia:平均年龄29
国家Croatia:平均年龄38
国家Thailand:平均年龄40
国家Australia:平均年龄29
国家Germany:平均年龄30
国家New Zealand:平均年龄34
open()函数
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
在Python3,可以通过open函数的newline参数来控制Universal new line mode:读取时候,不指定newline,则默认开启Universal new line mode,所有 , , or 被默认转换为 ;写入时,不指定newline,则换行符为各系统默认的换行符( , , or , ),指定为newline=' ',则都替换为 (相当于Universal new line mode);不论读或者写时,newline=''都表示不转换。