Home > Net >  Failing: Replace a single value with a new value for an individual DataFrame column
Failing: Replace a single value with a new value for an individual DataFrame column

Time:10-30

I am trying to replace some IDs in a column.

I am reading an excel file and storing it in a data frame:

Sample code:

file_loc1 = "(updated) Ambulance2Centroids_16622.xlsx"
df_A2C = pd.read_excel(file_loc1, index_col=None, na_values=['NA'])

Output of df_A2C.head().to_dict('list') for reproducibility:

{'FROM_ID': [1, 1, 1, 1, 1],
 'TO_ID': [7, 26, 71, 83, 98],
 'DURATION_H': [0.528555555555556,
  0.512511111111111,
  0.432452777777778,
  0.599486111111111,
  0.590516666666667],
 'DIST_KM': [38.4398, 37.38515, 32.57571, 39.26188, 35.53107]}

After this, I am checking to see the values which I want to replace using this code: df_A2C.loc[(df_A2C['FROM_ID'] == 9)]

This gives an output:

FROM_ID TO_ID   DURATION_H  DIST_KM FROM_ID
    9   7        1.183683   89.26777    9
    9   26       1.167639   88.21312    9
    9   71       1.087581   83.40369    9
    9   83       1.254614   90.08985    9
    9   98       1.245642   86.35904    9

Now, I am trying to replace FROM_ID values 9 with 8.

I have tried the following codes.

df_A2C['FROM_ID'] = df_A2C['FROM_ID'].replace('9','8')

Also,

df_A2C.loc[ df_A2C["FROM_ID"] == "9", "FROM_ID"] = "8" #df['column name'] = df['column name'].replace(['old value'], 'new value')

To test the results, I am doing this: df_A2C.loc[(df_A2C['FROM_ID'] == 8)]

output:

FROM_ID TO_ID   DURATION_H  DIST_KM FROM_ID

None of these are working.

I want to replace FROM_ID values 9 with 8. I do not want to create another column, just want to replace existing column values.

Am I making any mistakes here?

CodePudding user response:

Have you tried

df_A2C['FROM_ID'] = df_A2C['FROM_ID'].apply(lambda x: 8 if x==9 else x)

that is if your values are int

if your values are strings use this

df_A2C['FROM_ID'] = df_A2C['FROM_ID'].apply(lambda x: '8' if x=='9' else x)
  • Related