I have 2 CSV files: 'Data' and 'Mapping':
- 'Mapping' file has 4 columns:
Device_Name
,GDN
,Device_Type
, andDevice_OS
. All four columns are populated. - 'Data' file has these same columns, with
Device_Name
column populated and the other three columns blank. - I want my Python code to open both files and for each
Device_Name
in the Data file, map itsGDN
,Device_Type
, andDevice_OS
value from the Mapping file.
I know how to use dict when only 2 columns are present (1 is needed to be mapped) but I don't know how to accomplish this when 3 columns need to be mapped.
Following is the code using which I tried to accomplish mapping of Device_Type
:
x = dict([])
with open("Pricing Mapping_2013-04-22.csv", "rb") as in_file1:
file_map = csv.reader(in_file1, delimiter=',')
for row in file_map:
typemap = [row[0],row[2]]
x.append(typemap)
with open("Pricing_Updated_Cleaned.csv", "rb") as in_file2, open("Data Scraper_GDN.csv", "wb") as out_file:
writer = csv.writer(out_file, delimiter=',')
for row in csv.reader(in_file2, delimiter=','):
try:
row[27] = x[row[11]]
except KeyError:
row[27] = ""
writer.writerow(row)
It returns Attribute Error
.
After some researching, I think I need to create a nested dict, but I don't have any idea how to do this.
Device_Name
column is the key in both files, on this key I want to map Device_OS, GDN & Device_Type values from mapping file to data file. – atamsrow[27] = x[row[11]]["Device_OS"]
? – Janne KarilaDevice_Name
the index, then you can directlyjoin
the two dataframes on their indexDevice_Name
. – smci