#πŸ”’ Not able to replace a value in a column with another values

41 messages Β· Page 1 of 1 (latest)

loud linden
#

In the columns "Diabetes_012", i want to replace "1" with "Diabetes". My code is not doing any changes in the dataset for the particular column

import pandas as pd 

# reading the csv file 
df = pd.read_csv("diabetes.csv") 

# updating the column value/data 
df['Diabetes_012'] = df['Diabetes_012'].replace({"1": 'Diabetes'}) 

# writing into the file 
df.to_csv("diabetes1.csv", index=False) 

print(df) 
dense latchBOT
#

Hey @loud linden!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
dense latchBOT
#

@loud linden

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

timid lily
loud linden
#

I need "1" to be changed here

timid lily
loud linden
# timid lily As text, please. Not a screenshot.

{'Diabetes_012': ['No Diabetes', 'No Diabetes', 'No Diabetes', 'No Diabetes', 'No Diabetes'], 'HighBP': [1.0, 0.0, 1.0, 1.0, 1.0], 'HighChol': [1.0, 0.0, 1.0, 0.0, 1.0], 'CholCheck': [1.0, 0.0, 1.0, 1.0, 1.0], 'BMI': [40.0, 25.0, 28.0, 27.0, 24.0], 'Smoker': [1.0, 1.0, 0.0, 0.0, 0.0], 'Stroke': [0.0, 0.0, 0.0, 0.0, 0.0], 'HeartDiseaseorAttack': [0.0, 0.0, 0.0, 0.0, 0.0], 'PhysActivity': [0.0, 1.0, 0.0, 1.0, 1.0], 'Fruits': [0.0, 0.0, 1.0, 1.0, 1.0], 'Veggies': [1.0, 0.0, 0.0, 1.0, 1.0], 'HvyAlcoholConsump': [0.0, 0.0, 0.0, 0.0, 0.0], 'AnyHealthcare': [1.0, 0.0, 1.0, 1.0, 1.0], 'NoDocbcCost': [0.0, 1.0, 1.0, 0.0, 0.0], 'GenHlth': [5.0, 3.0, 5.0, 2.0, 2.0], 'MentHlth': [18.0, 0.0, 30.0, 0.0, 3.0], 'PhysHlth': [15.0, 0.0, 30.0, 0.0, 0.0], 'DiffWalk': [1.0, 0.0, 1.0, 0.0, 0.0], 'Sex': ['Female', 'Female', 'Female', 'Female', 'Female'], 'Age': [9.0, 7.0, 9.0, 11.0, 11.0], 'Education': [4.0, 6.0, 4.0, 3.0, 5.0], 'Income': [3.0, 1.0, 8.0, 6.0, 4.0]}

timid lily
loud linden
#
import pandas as pd 

# reading the csv file 
df = pd.read_csv("diabetes.csv") 

# updating the column value/data 
df['Diabetes_012'] = df['Diabetes_012'].replace({1: 'Diabetes'}) 

# writing into the file 
df.to_csv("diabetes1.csv", index=False) ```
dense latchBOT
#

Hey @loud linden!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
loud linden
#

tried this

#

but not chnaging

#

this is the dataset

dense latchBOT
# loud linden

Please react with βœ… to upload your file(s) to our paste bin, which is more accessible for some users.

timid lily
#

@loud linden okay, as an alternative, you can use .loc

#

!docs pandas.DataFrame.loc

dense latchBOT
#

property DataFrame.loc```
Access a group of rows and columns by label(s) or a boolean array.

`.loc[]` is primarily label based, but may also be used with a boolean array.

Allowed inputs are:
timid lily
#
In [5]: df
Out[5]:
   a  b
0  1  4
1  2  5
2  3  6

In [7]: df.loc[df['a'] == 2, 'a'] = -1

In [8]: df
Out[8]:
   a  b
0  1  4
1 -1  5
2  3  6
#

@loud linden take a look at this example and see if you can intuit what loc is doing.

real meadow
#

if .replace didn't have an effect, .loc one won't either

#

try inspecting the data before doing anything first perhaps, such as df["Diabetes_012"].unique(), df["Diabetes_012"].value_counts(dropna=False) etc.

#

and see what values there are really, if it has 1s, "1"s or something elses

real meadow
#

so it turns out you have "1.0" in that column for some reason

#

you see that right?

#

the .unique output shows it, the value counts show how many there are

#

now it's your turn to do whatever you want to do

loud linden
real meadow
#

ok you can do that with .replace you had

#

thing you want replaced is now known, it is "1.0"

#

previously you had "1"

#

!e print("1" == "1.0")

dense latchBOT
real meadow
#

since they are not the same, it wasn't getting replaced

#

now you can put "1.0" instead of "1" and try again

dense latchBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.