[ Applied Data Science with Python]week1-part2

[ Applied Data Science with Python]week1-part2

string 格式化输出

sales_record = {
'price': 3.24,
'num_items': 4,
'person': 'Chris'}

sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}'

print(sales_statement.format(sales_record['person'],
                             sales_record['num_items'],
                             sales_record['price'],
                             sales_record['num_items']*sales_record['price']))

读写CSV文件

import csv
%precision 2
with open('mpg.csv') as csvfile:
    mpg = list(csv.DictReader(csvfile))   
mpg[:3] # The first three dictionaries in our list.
len(mpg)
#column names of our csv.
mpg[0].keys()
#the average cty fuel economy across all cars
sum(float(d['cty']) for d in mpg) / len(mpg)
#set,返回唯一值
cylinders = set(d['cyl'] for d in mpg)
#按照油缸数,求城市内平均每加仑汽油行驶的公里数
CtyMpgByCyl = []

for c in cylinders: # iterate over all the cylinder levels
    summpg = 0
    cyltypecount = 0
    for d in mpg: # iterate over all dictionaries
        if d['cyl'] == c: # if the cylinder level type matches,
            summpg += float(d['cty']) # add the cty mpg
            cyltypecount += 1 # increment the count
    CtyMpgByCyl.append((c, summpg / cyltypecount)) # append the tuple ('cylinder', 'avg mpg')

CtyMpgByCyl.sort(key=lambda x: x[0])
CtyMpgByCyl

时间

import datetime as dt
import time as tm
#返回距离1970年1月1日的时间(以秒为单位)
tm.time()
#将时间戳转换为日期
dtnow = dt.datetime.fromtimestamp(tm.time())
dtnow
#日期的属性
dtnow.year, dtnow.month, dtnow.day, dtnow.hour, dtnow.minute, dtnow.second # get year, month, day, etc.from a datetime

#时间增量(timedelta)是表示两个日期之间差异的持续时间。
delta = dt.timedelta(days = 100) # create a timedelta of 100 days
delta

#获取今天日期
today = dt.date.today()
#datetime.date(2019, 4, 24)
#100天前日期
today - delta # the date 100 days ago
#datetime.date(2019, 1, 14)
today > today-delta # compare dates
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值