I have a python dict/hashmap:
yearMapping = {"2016":"6", "2017":"7", "2018":"8", "2019":"9"}
I am trying to replace values within a DataFrame object with the corresponding map value. I wrote out an example below with my expected output.
I have 3 columns with data in them. If generated code is null, I am going to populate that column with the Contract Time + the map value.
Generated Code | Contract Date | Contract Time
null | 201607 | 1:31:01
Expected output:
Generated Code | Contract Date | Contract Time
1:31:016 | 201607 | 1:31:01
What I am doing so far:
yearCode = df['Contract Date'].astype(str).apply(lambda x: x[:4])
df.loc[df["Generated Code"].isnull(),'Generated Code'] = df['Contract Time'] + yearMapping.get(yearCode)
I keep getting the error: TypeError: 'Series' objects are mutable, thus they cannot be hashed
Is this even do-able?
I think you need
replace
fromnull
toNaN
, then indexing with str and lastSeries
yearCode
map
bydict
:Another solution is change condition: