0

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 ^

8
  • 1
    try inplace or assigning that to df again Commented Feb 21, 2018 at 13:29
  • tried inplace=True but no luck Commented Feb 21, 2018 at 13:29
  • Please see the edited answer. Commented Feb 21, 2018 at 13:32
  • Did you read the documentation on replace? Because with the mapping you never search for "pave" or whatever... you search for each of the keys in the mapping dict in each of the columns and try to replace the occurence of "street, LotShape, ..." with 1,2,... Commented Feb 21, 2018 at 13:38
  • I have tried doing.....mapping={'Pave':1,'Lvl':2,'LandContour':3,'Utilities':4,'SaleCondition':5} #df.replace('Street',0) df.replace({'Street': mapping, 'LotShape': mapping, 'LandContour': mapping, 'Utilities': mapping, 'SaleCondition': mapping},True)...but that didn't work either Commented Feb 21, 2018 at 13:40

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

YOU ARE THE LIFE SAVIOUR :)
Happy to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.