TVP

# 早下班系列：比Python更称手的兵器

import pandas

f = open(‘D:/data.txt’, ‘r‘)

try:

a = f.read( )

finally:

f.close( )

b = []

for i in a.split(“\n”):

b.append(i.split(” “))

for i in range(1, len(b)):

b[i][4] = b[i][4].replace(“\$”,‘ ‘)

b[i][4] = b[i][4].replace(“,”,‘ ‘)

for i in range(1, len(b)):

for j in [1, 2, 3, 4]:

b[i][j] = eval(b[i][j])

data = pandas.DataFrame(b[1:],columns=b[0])

out = data.groupby([‘STYLE’,‘BEDROOMS’]).mean()

print(out)

import pandas

import time

start = time.clock()

f = open(‘D:/data2.txt‘, ‘r’)

try:

a = f.read( )

finally:

f.close( )

b = []

for i in a.split(“\n”):

b.append(i.split(” “))

for i in range(1, len(b)):

b[i][4] = b[i][4].replace(“\$”,‘ ‘)

b[i][4] = b[i][4].replace(“,”,‘ ‘)

for i in range(1, len(b)):

for j in [1, 2, 3, 4]:

b[i][j] = eval(b[i][j])

data = pandas.DataFrame(b[1:],columns=b[0])

out = data.groupby([‘STYLE’,‘BEDROOMS’]).mean()

elapsed = (time.clock() – start)

print(elapsed)

1391毫秒约等于1.4秒（请注意此处有个小数点……）

• 发表于:
• 原文链接http://kuaibao.qq.com/s/20180111A0YGJ300?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2023-02-05

2023-02-05

2023-02-05

2023-02-05

2023-02-05

2023-02-05

2018-05-11

2023-02-05

2023-02-05

2019-09-09

2023-02-05

10元无门槛代金券