在列表中搜索数据并将其导出到.csv文件

时间:2018-08-19 13:27:41

标签: python algorithm

我已经完成了代码的另一部分,并获得了两组数字:

set1(西澳大利亚州的邮政编码和租金):

[['6004', '240'], ['6004', '350'], ['6004', '350'], ['6004', '315'], ['6004', '490'], ['6004', '280'], ['6004', '275'], ['6004', '240'], ['6050', '260'], ['6050', '330'], ['6050', '220'], ['6050', '250'], ['6000', '390'], ['6000', '220'], ['6000', '400'], ['6000', '250'], ['6000', '320'], ['6000', '390'], ['6000', '220'], ['6000', '400'], ['6004', '395'], ['6004', '230'], ['6004', '350'], ['6004', '300'], ['6004', '300'], ['6004', '340'], ['6000', '420'], ['6000', '190'], ['6000', '300'], ['6000', '380'], ['6000', '270'], ['6000', '380'], ['6000', '350'], ['6000', '380'], ['6004', '360'], ['6004', '450'], ['6004', '200'], ['6004', '250'], ['6004', '350']]

set2(set1中的邮政编码):

['6004', '6050', '6000']

我现在要做的是将它们放入.csv文件中,如下所示:

enter image description here

邮政编码位于第一行(如索引)。其余各行是这些邮政编码中的租金(例如:邮政编码6004中有3栋房屋,租金为240,350,350)

我应该使用哪种方法来获得想要的东西?

我尝试过字典,但它说只需要2个元素。

2 个答案:

答案 0 :(得分:1)

给出:

postcodes_rent=[['6004', '240'], ['6004', '350'],.......]
postcodes=['6004', '6050', '6000']

创建字典:

postcodes_rent_dict={p:[pr[1] for pr in postcodes_rent if pr[0]==p] for p in postcodes}
{'6004': ['240', '350', '350'.....], 
 '6050': ['260', '330', '220'.....], 
 '6000': ['390', '220', '400'.....]}

将其转换为数据框(以邮政编码作为列名):

import pandas as pd
df=pd.DataFrame.from_dict(postcodes_rent_dict,orient='index').transpose()

将其写入csv文件

df.to_csv("test.csv")

答案 1 :(得分:0)

仅使用内置函数并使用zip_longest转置的方法:

import csv
from itertools import zip_longest

data = [['6004', '240'], ['6004', '350'], ['6004', '350'], ['6004', '315'], ['6004', '490'], ['6004', '280'], ['6004', '275'], ['6004', '240'], ['6050', '260'], ['6050', '330'], ['6050', '220'], ['6050', '250'], ['6000', '390'], ['6000', '220'], ['6000', '400'], ['6000', '250'], ['6000', '320'], ['6000', '390'], ['6000', '220'], ['6000', '400'], ['6004', '395'], ['6004', '230'], ['6004', '350'], ['6004', '300'], ['6004', '300'], ['6004', '340'], ['6000', '420'], ['6000', '190'], ['6000', '300'], ['6000', '380'], ['6000', '270'], ['6000', '380'], ['6000', '350'], ['6000', '380'], ['6004', '360'], ['6004', '450'], ['6004', '200'], ['6004', '250'], ['6004', '350']]
postcodes = ['6004', '6050', '6000']
# convert to a set for O(1) lookup
pcs = set(postcodes)

# Accumulate all rent costs per postcode
pc_rents = {}
for pc, rent in data:
    # ignore unwanted postcodes...
    if pc not in postcodes:
        continue
    pc_rents.setdefault(pc, []).append(rent)

# Write transposed rows...
with open('output.csv', 'w') as fout:
    csvout = csv.DictWriter(fout, fieldnames=postcodes)
    csvout.writeheader()
    csvout.writerows(
        # build dictionary with field names expected
        dict(zip(pc_rents, row)) 
        # transposed rows...
        for row in zip_longest(*pc_rents.values(), fillvalue='')
    )

为您提供以下CSV文件:

6004,6050,6000
240,260,390
350,330,220
350,220,400
315,250,250
490,,320
280,,390
275,,220
240,,400
395,,420
230,,190
350,,300
300,,380
300,,270
340,,380
360,,350
450,,380
200,,
250,,
350,,