Reputation:
I am trying to replace some IDs in a column.
I am reading an excel file using pd.read_excel
and storing it in a data frame:
df_A2C = pd.read_excel(file_loc1, index_col=None, na_values=['NA'])
This can be reproduced with
df_A2C = pd.DataFrame({'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"
To test the results, I am doing 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?
Upvotes: 2
Views: 53
Reputation: 17730
Your solution based on replace
works, I guess the problem is somewhere else (e.g. you have two columns labelled FROM_ID
)
Let use the input example:
df_A2C = pd.DataFrame({'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]})
unfortunately there is no record with FROM_ID
equal to 8, but all of them are equal to 1. So let assume you want to change 1 to 100.
df_A2C['FROM_ID'] = df_A2C['FROM_ID'].replace(1, 100)
then df_A2C
is represented as
FROM_ID TO_ID DURATION_H DIST_KM
0 100 7 0.528556 38.43980
1 100 26 0.512511 37.38515
2 100 71 0.432453 32.57571
3 100 83 0.599486 39.26188
4 100 98 0.590517 35.53107
as expected. And you can also call df_A2C.loc[(df_A2C['FROM_ID'] == 100)]
which returns the same result.
Upvotes: 0
Reputation: 487
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)
Upvotes: 2