Home > Back-end >  Panda Dataframe Series Replace value
Panda Dataframe Series Replace value

Time:04-15

I have a program in Python:

import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')

# Read a CSV file:
name = pd.read_csv("users.csv", delimiter=";") 

# Get the value of the row with index 0, column "Name"
name = users.iloc[0].get("Name")

# Supposedly replace the first user Name to np.NaN
users.iloc[0].replace(name , np.NaN, inplace=True)

I would expect this to replace the name, but it doesn't. The way to replace it, is:

Replace every name "name" with np.NaN ?
users.replace(name, np.NaN, inplace=True)

But isn't the previous line going to replace the name for the whole file? how to do the replacement for the row only?

CodePudding user response:

When you do a replacement on df.iloc[i], it doesn't persist back to the source data frame. Here's an example:

df = pd.DataFrame({'A': [1]})
Out[1]: 
A    1
Name: 0, dtype: int64

Replace appears to work.

df.iloc[0].replace(1, 5)
Out[2]: 
A    5
Name: 0, dtype: int64

But it doesn't actually get sent back to the original data frame.

df
Out[3]: 
   A
0  1

You would need to reassign the output of your replace statement back to the proper location in the data frame. Something like this:

df.iloc[0] = df.iloc[0].replace(1, 5)
df
Out[57]: 
   A
0  5
  • Related