Python 不替换数据帧中的字符串

Python 不替换数据帧中的字符串,python,pandas,dataframe,Python,Pandas,Dataframe,我看到了这个问题,但它对我不起作用,我确信我犯了一个错误,但请告诉我哪里做错了,我希望“Street”、“LandContour”等值从“pave”替换为1等等 这是我到目前为止的代码: 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)

我看到了这个问题,但它对我不起作用,我确信我犯了一个错误,但请告诉我哪里做错了,我希望“Street”、“LandContour”等值从“pave”替换为1等等

这是我到目前为止的代码:

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()
我尝试了
df['Street'].replace(“pave”,0,inplace=True)
和许多其他方法,但都不起作用。甚至df.replace中给出的参数的单个值也没有被替换。我的df工作正常,它正在打印头部和特定列,
df.fillna
也工作正常。任何帮助都会很好

编辑:所有未注释的行都正常工作,我希望未注释的行正常工作

示例输出为:-

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  
我也尝试过:-

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})
但是那也不行^

试试:

df = pd.read_csv('train.csv')                  # reset
df.fillna(-99999, inplace=True)                # refill
df['Street'].replace('Pave', 0, inplace=True)  # replace
以前的方法的问题是,它们没有使用正确的搜索值将replace应用于正确的列。也要注意资本化。

试试:

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

以前的方法的问题是,它们没有使用正确的搜索值将replace应用于正确的列。也要注意大写。

尝试使用inplace或将其指定给df againtried inplace=True,但没有运气请查看编辑后的答案。您是否阅读了有关替换的文档?因为有了地图,你永远不会搜索“铺路”或其他什么。。。在每个列中搜索mapping dict中的每个键,并尝试将出现的“street,LotShape,…”替换为1,2({'Street':mapping,'LotShape':mapping,'LandContour':mapping,'Utilities':mapping,'SaleCondition':mapping},True)…但这不起作用,或者将其分配给df againtried inplace=True,但没有运气请查看编辑后的答案。您是否阅读了关于replace的文档?因为使用该映射,您永远不会搜索“pave”或者随便什么……您在每个列中搜索映射dict中的每个键,并尝试将“street,LotShape,…”的出现替换为1,2,….我已经尝试过……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)…但这不起作用。你是救命恩人:)乐意帮忙:)你是救命恩人:)乐意帮忙:)