python - Replace items in column based on list -
i have dataframe so:
date value 19990506 0.6 19990506 0.8 19990607 1.2 20000802 0.4
and have 2 lists this:
list1 = ['19990506', '19990607', '20000802'] list2 = ['1999201', '1999232', '2000252']
the items in list1
coincide values in column date
, want replace them items in list2
. in date
19990506
replaced 1999201
, 19990607
replaced 1999232
. think need zip lists this, after @ loss on best way go it. showing simplified dataframe using .replace
not efficient me. desired output this:
date value 1999201 0.6 1999201 0.8 1999232 1.2 2000252 0.4
if create dictionary maps list1
list2
can use series.map
:
df = pd.read_clipboard() list1 = ['19990506', '19990607', '20000802'] list2 = ['1999201', '1999232', '2000252'] # when read in data ints in date column # need ints in replacement map, if have # strings remove int() conversions replacement_map = {int(i1): int(i2) i1, i2 in zip(list1, list2)} df['date'] = df['date'].map(replacement_map) df out[13]: date value 0 1999201 0.6 1 1999201 0.8 2 1999232 1.2 3 2000252 0.4
Comments
Post a Comment