Asim
Asim

Reputation: 1490

Pandas not replacing strings in dataframe

I have seen this question but it isn't working for me, I am sure I am making a blunder but please tell me where I am doing wrong, I want values "Street", "LandContour" etc to be replaced from "pave" to 1 and so on.

python pandas replacing strings in dataframe with numbers

This is my code till now:

import numpy as np
import pandas as pd

df=pd.read_csv('train.csv')       # getting file

df.fillna(-99999, inplace=True)

#df.replace("Street", 0, True)    didn't work

# mapping={'Street':1,'LotShape':2,'LandContour':3,'Utilities':4,'SaleCondition':5}

# df.replace('Street', 0)  # didn't work

# df.replace({'Street': mapping, 'LotShape': mapping, 
#            'LandContour': mapping, 'Utilities': mapping,
#            'SaleCondition': mapping})
# didn't work ^
df.head()

I have tried df['Street'].replace("pave",0,inplace=True) and a lot of other things but none worked. Not even the single value of arguments given in df.replace are replaced. My df is working fine it is printing the head and also specific coloumns, df.fillna also worked fine. Any help will be great.

EDIT: All the non-commented lines are working and I want uncommented lines to work.

The sample output is:-

Id  MSSubClass MSZoning  LotFrontage    LotArea     Street   Alley LotShape  \
0   1          60       RL         65.0     8450   Pave  -99999      Reg   
1   2          20       RL         80.0     9600   Pave  -99999      Reg   
2   3          60       RL         68.0    11250   Pave  -99999      IR1   
3   4          70       RL         60.0     9550   Pave  -99999      IR1   
4   5          60       RL         84.0    14260   Pave  -99999      IR1   

  LandContour Utilities    ...     PoolArea  PoolQC   Fence MiscFeature  \
0         Lvl    AllPub    ...            0  -99999  -99999      -99999   
1         Lvl    AllPub    ...            0  -99999  -99999      -99999   
2         Lvl    AllPub    ...            0  -99999  -99999      -99999   
3         Lvl    AllPub    ...            0  -99999  -99999      -99999   
4         Lvl    AllPub    ...            0  -99999  -99999      -99999   

  MiscVal MoSold YrSold  SaleType  SaleCondition  SalePrice  
0       0      2   2008        WD         Normal     208500  
1       0      5   2007        WD         Normal     181500  
2       0      9   2008        WD         Normal     223500  
3       0      2   2006        WD        Abnorml     140000  
4       0     12   2008        WD         Normal     250000  

I have also tried:-

mapping={'Pave':1,'Lvl':2,'AllPub':3,'Reg':4,'Normal':5,'Abnormal':0,'IR1':6}

#df.replace('Street',0)

df.replace({'Street': mapping, 'LotShape': mapping, 
'LandContour': mapping, 'Utilities': mapping, 'SaleCondition': mapping})

But that didnt work either ^

Upvotes: 0

Views: 212

Answers (1)

David Stevens
David Stevens

Reputation: 865

Try:

df = pd.read_csv('train.csv')                  # reset
df.fillna(-99999, inplace=True)                # refill
df['Street'].replace('Pave', 0, inplace=True)  # replace

The problem with your previous approaches is that they don't apply replace to the correct column with the correct search values. Pay careful attention to capitalization as well.

Upvotes: 3

Related Questions