People OS Games Owned
Anthony Windows 120
Alissa Windows 70
Jordan Windows 20
Khan Mac 47
Benny Mac 23
Anastasia Linux 16
McCurdy Linux 10
我想知道,我如何过滤掉拥有20多个游戏的人,而且他们没有Mac OS系统。我需要通过python脚本来完成它,并且在运行时,它将其数据输出到单独的文件中,如文本文件或其他东西。谢谢!
答案 0 :(得分:1)
我建议使用Pandas库。
代码基本如下:
import pandas as pd
data = pd.read_csv('put in your csv filename here')
# Filter the data accordingly.
data = data[data['Games Owned'] > 20]
data = data[data['OS'] == 'Mac']
答案 1 :(得分:1)
这是纯python中的一个解决方案,它根据请求将过滤后的输出写入文本文件(csv)。
import csv
with open('games.csv', 'rb') as csvfile:
# handle header line, save it for writing to output file
header = next(csvfile).strip("\n").split(",")
reader = csv.reader(csvfile)
results = filter(lambda row: row[1] != 'Mac' and int(row[2]) > 20, reader)
with open('output.csv', 'wb') as outfile:
writer = csv.writer(outfile)
writer.writerow(header)
for result in results:
writer.writerow(result)