简体   繁体   中英

Replace values using loc command

There's a dataframe and I need to replace values above 512 with 263.

So, I used this code line to filter my indexes first:

df.loc[df['Fare']>512]['Fare'].astype(int)

Here is the result of this:

258     512
679     512
737     512
1234    512
Name: Fare, dtype: int64

This looks good! as it filtered all the 4 rows with a value above 512. Now I need to replace this value with 263:

df.loc[df['Fare']>512]['Fare']=df.loc[df['Fare']>512]['Fare'].astype(int).replace({512:263},inplace=True)

But it doesn't change anything in my dataframe. For instance, when I search for index 737, I found this:

df.iloc[737]

Result:

Age                                35
Fare                          512.329

So despite of above codes, the Fare hasn't been changed to 263.

when using .loc you want to use [row, col] and not [row][col] .

try:

df.loc[df['Fare']>512, 'Fare']=df.loc[df['Fare']>512, 'Fare'].astype(int).replace({512:263},inplace=True)

Is there any reason why you aren't just doing

condition = df['Fare'].astype(int) > 512
df.loc[condition, 'Fare'] = 263

The condition is a boolean series and .loc will only assign rows in that series with value True to your required value.

Remove the inplace = True option.

df.loc[df['Fare']>512]['Fare']=df.loc[df['Fare']>512]['Fare'].astype(int).replace({512:263})

or simply do not assing.

df.loc[df['Fare']>512]['Fare'].astype(int).replace({512:263}, inplace=True)

From the replace docs :

inplace : bool, default False
If True, in place. Note: this will modify any other views on this object (eg a column from a DataFrame). Returns the caller if this is True.

By now, you are modifing the dataframe inplace, but the assignment operator = return the caller, so you are rewriting your edit with the original values.

EDIT

Actually in my version (pandas 0.24.0) with inplace = True it does not return anything, so the bold sentence above may be version dependent (the docs refers to pandas 0.24.2).

As a side note, filtering the data with .loc and then using replace is redundant: .replace({512:263}) will convert values 512 only, no need to select that values before with .loc .
If you do:

df['Fare'].astype(int).replace({512:263}, inplace=True)

you get the same result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM