【头歌】综合训练项目1:Python数据分析基础

第1关:读取MoMA数据集

from csv import reader

# Read the `artworks_clean.csv` file
opened_file = open('artworks_clean.csv', encoding='utf-8')
read_file = reader(opened_file)
moma = list(read_file)
moma = moma[1:]

# Convert the birthdate values
for row in moma:
    birth_date = row[3]
    if birth_date != "":
        birth_date = int(birth_date)
    row[3] = birth_date
    
# Convert the death date values
for row in moma:
    death_date = row[4]
    if death_date != "":
        death_date = int(death_date)
    row[4] = death_date

# 请在此添加代码,创建字典然后打印结果
#********** Begin **********#
for row in moma:
    date = row[6]
    if date != "":
        date = int(date)
    row[6] = date
print(type(moma[100][6]))
#********** End **********#

第2关:计算艺术家年龄

from csv import reader

# Read the `artworks_clean.csv` file
opened_file = open('artworks_clean.csv', encoding='utf-8')
read_file = reader(opened_file)
moma = list(read_file)
moma = moma[1:]

# Convert the birthdate values
for row in moma:
    birth_date = row[3]
    if birth_date != "":
        birth_date = int(birth_date)
    row[3] = birth_date

# Convert the death date values
for row in moma:
    death_date = row[4]
    if death_date != "":
        death_date = int(death_date)
    row[4] = death_date

# Convert the date values
for row in moma:
    date = row[6]
    if date != "":
        date = int(date)
    row[6] = date

# 请在此添加代码,计算艺术家年龄然后打印结果
#********** Begin **********#
ages=[]
final_ages=[]
for row in moma:
    date = row[6]
    birth = row[3]
    if type(birth) == int:
        age = date - birth
    else:
        age = 0
    ages.append(age)
for a in ages:
    if a >20:
        final_ages.append(a)
    else:
        final_ages.append('Unknown'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值