Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Panda Dataframe Series Replace value

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading