Muhammad Ashfaq
Muhammad Ashfaq

Reputation: 94

Pandas replace method not working on column

Pretty simple code. The program seems to completely ignore the replace line. I am looking to move the values of 0, 5, 6 to the value '4' (Assign all elements with values as 0 or 5 or 6 as a 4). This is the column info. I have tried removing "value" and even "inplace" (I know, that gives a new object, however, even then nothing happens and it remains the same)

2    14030 
1    10585
3     4917
5      280
4      123
6       51
0       14
Name: EDUCATION, dtype: int64


import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv('UCI_Credit_Card.csv')
graph = data[['LIMIT_BAL','AGE']].describe()
data['EDUCATION'].replace(['0', '5', '6'], value='4', inplace=True)
print(data['EDUCATION'].value_counts())

Upvotes: 1

Views: 47

Answers (2)

BENY
BENY

Reputation: 323226

You need to turn the regex on

data['EDUCATION'] = data['EDUCATION'].astype(str).replace(['0', '5', '6'], value='4', regex=True)

Upvotes: 1

Bharath
Bharath

Reputation: 416

A simple solution, assuming all of the values are integers. I have recreated your dataframe and put a replace method

df = pd.DataFrame([[2,1,3,5,4,6,0],[14030,10585,4917,280,123,51,14]])
df = df.T
print(df)
df.replace([0,5,6],0)

Upvotes: 0

Related Questions