0

I'm trying to execute a simple operation of replacing certain values in my dataframe with others. Essentially, the datetime variables are in string format, and must be converted into datetime format. But there are typos in the variables, such as 3014 instead of 2014, which causes the datetime format conversion to break.

It should be a quick fix to replace all instances of "3014" with "2014" but whenever I try to run the code using replace(), all I end up doing is erasing the entire column of data. Below is the broken code I'm using.

df['Date_Time'].replace(['3014', "2914", '2081'], ['2014', "2014", '2018'], inplace=True)

I have also tried using df.loc[] to achieve the same effect, to no avail. Also, this alternative does not seem to easily let me pass a list of values to replace. Instead, I would have to write a line of code per value I need replacing, which is tedious at best. Below is my attempt at using .loc:

df.loc[(df['Date_Time_Arrest'] == '3014'), 'Date_Time_Arrest'] = '2014'

Are there any other alternatives to quickly replace multiple values in a dataframe? Even when I ensure the columns are in string format, I still cannot replace values without erasing the entire column of data for some reason.

JuBru
  • 1

1 Answers1

1
mapping = dict(zip(['3014', "2914", '2081'], ['2014', "2014", '2018'])
df['Date_Time'] = df['Date_Time'].map(mapping)

NB: avoid using inplace https://stackoverflow.com/a/59242208/11472761

Riley
  • 2,153
  • 1
  • 6
  • 16