0

I have a dictionary named cities with key value pair; zip-code:city and a numpy array named arrRent with lists of 16 rents for each zip-code. What I want to do is to pair the city with a list of rents into another dictionary.

My code:

def cityPriceDict(self):
    count = 0
    cityprices = {}
    for key, city in self.cities.items():
        cityprices[city] = self.arrRent[count]
        count += 1

When I print the cityprices, it only prints out the last city in the dictionary paired with the first rent list. Not sure why any help would be appreciated.

zipCity.csv:

95136,San Jose

95134,San Jose

95131,San Jose

95129,San Jose

95128,San Jose

95126,San Jose

95125,San Jose

95123,San Jose

95117,San Jose

95112,San Jose

95110,San Jose

95054,Santa Clara

95051,Santa Clara

95050,Santa Clara

95035,Milpitas

95032,Los Gatos

95014,Cupertino

95008,Campbell

94306,Palo Alto

94089,Sunnyvale

94087,Sunnyvale

94085,Sunnyvale

94043,Mountain View

94040,Mountain View

rent.csv:

2659,2623.5,2749.5,2826.5,2775,2795,2810,2845,2827,2847,2854,2897.5,2905,2925,2902.5,2869.5

3342.5,3386,3385,3353,3300,3190,3087.5,3092,3170,3225,3340,3315,3396,3470,3480,3380

2996,2989,2953,2950,2884.5,2829,2785,2908,2850,2761,2997.5,3020,2952,2997.5,2952,2923.5

2804.5,2850.5,2850,2850,2867,2940,2905,2945,2938,2860,2884,2946,2938,2986.5,2931.5,3032.5

2800,3074,2950,2850,2850,2875,2757,2716,2738.5,2696,2809,2891,3000,2960,2950,2831

3215,3250,3290,3260,3200,3350,3507.5,3301,3276,3320,3550,3500,3530,3498.5,3505,3605

2597.5,2649,2625.5,2890,3325,3200,3010,2850,2800,2745,2695,2695,2850,2850,2860,2695

2783.5,2800,2812,2809,2813.5,2817,2815,2849,2850,2927,2810,2890,2910,2996,2950,2897

2350,2350,2350,2475,2400,2495,2567.5,2525,2400,2350,2487.5,2395,2397.5,2450,2562.5,2500

3152.5,3015,3060,3027.5,2950,3000,3002.5,3022.5,2865,2850,2825,2895,3020,3022.5,3110,3185

3337,3500,3484,3519,3393.5,3295,3062.5,3057,3123.5,3103,3079,3216,3221,3200,3214,3405.5

3180,3325,3495,3488.5,3271.5,3216,3235,3216,3300,3405,3388.5,3600,3915,3629.5,3541,3405

2800,3049,3000,3032,2995,2987.5,2850,3000,3000,2895,2992.5,3080,3378.5,3094.5,3056,3150

2871.5,2850,2751,2710,2800,2875,2845,2700,2784.5,2749,2800,2875,2905,3028,3100,3100

3552.5,3550,3540,3510,3495,3510,3512.5,3525,3300,3270,3250,3182.5,3200,3200
,3250,3200

3397.5,3389,3450,3400,3300,3500,3495,3497.5,3395,3595,3350,3350,3425,3399.5,3364.5,3325.5

3550,3472,3493,3344.5,3332,3251,3270.5,3358,3370.5,3465.5,3495,3495,3500,3495,3458,3550

2805,2833,2900,2930,2795,2800,2890,2949,2800,2850,2839,2900,2850,2995,3087.5,2950.5

3495,3530,3610,4200,3990,3925,4000,3785,3792.5,3525,3495,3850,3900,4137.5,4000,4200

3545,3355,3305,3143,3220,3200,3180,3639,3260,3265,3510,3575,3695,3482.5,3600,3292.5

2995,3180,3225,3204,3219,3109,2998.5,2995,3200,3193.5,3161,3195,3200,3338.5,3200,3176

3599,3641,3796,3650,3552.5,3570,3535,3465,3400,3316,3640,3770,3440,3790,3815,3692.5

3500,3509.5,3519,3717.5,3495,3435,3285,3162.5,3425,3375,3410,3472.5,3600,3980,3657.5,3650

3773,3696,3708,3778,3689,3625,3516,3518.5,3647,3596,3685,3945,3811,3848,3699,4021.5

1 Answer 1

1

May be this will work for you. Please let me.know if it doesn't.

New_dict = dict(zip(self.cities.values(),self.arrRent))
print(New_dict)

All the best.

Edit: Suggestion to read the file added I am not aware of the file contents and layout but I would have started troubleshooting with the below code. Please change to suit the requirement.

self.city = {}
with open('zipCity.csv') as fh:
    reader = csv.reader(fh)
    for row in reader:
        self.city[row[0]] = row[1]

Edit Incorporating answer to additional request

Hopefully I got what you wanted but I am convinced there is a better and elegant way to achieve this. My code is as below.

import pandas as pd
import numpy as np

ZipCodes_DF = pd.read_csv("./zipCodes.csv",header=None,names=['ZipCode','City'])
ArrRent_DF = pd.read_csv("./arrRent.csv",header=None,names=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'])
Merged_DF = pd.concat([ZipCodes_DF,ArrRent_DF],axis=1)

Grouped_DF = Merged_DF.groupby(by=['City'])
Grouped_DF_Merged = Grouped_DF['a'].apply(list)
for eachCol in ['b','c','d','e','f','g','h','i','j','k','l','m','n','o','p']:
    Grouped_DF_Merged = pd.concat([Grouped_DF_Merged,Grouped_DF[eachCol].apply(list)],axis=1)
Grouped_DF_Merged_One = Grouped_DF_Merged.apply(lambda x: np.array([a for b in x for a in b]).flatten(),axis=1)
print(Grouped_DF_Merged_One)

The output of this script is as below

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

This doesnt work either. It prints the same thing as my code, the last city with the value as the first rent list.
What is the output of print(len(self.cities.values())) and print(len(self.arrRent))? If everything looks good then perhaps printing the self.cities and self.arrRent will give some clue!!
Oh I see the problem, the dictionary is only storing the last city from the csv file for some reason. Can you help me fix my dictionary? This is the code that I have right now for reading and storing it: with open('zipCity.csv') as fh: reader = csv.reader(fh) for row in reader: self.city = {row[0]: row[1]}
I added the edit. What I think is happening is that self.city is getting overwritten every time. Please check that.
I have another question. Sometimes the zipCity.csv file contains multiple zipcodes for a single city and I'm wondering how I would zip the different zipcodes with their own separate rent but keep the zipcodes under their cities "category". I'll include the csv files in my original post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.