【问题标题】:Pandas not replacing strings in dataframe熊猫不替换数据框中的字符串
【发布时间】:2018-08-01 02:36:40
【问题描述】:

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

python pandas replacing strings in dataframe with numbers

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

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
  • 尝试就地=真但没有运气
  • 请查看编辑后的答案。
  • 您是否阅读了有关替换的文档?因为使用映射,您永远不会搜索“pave”或其他任何内容......您在每个列中搜索映射字典中的每个键,并尝试用 1 替换“street, LotShape, ...”的出现,2,...
  • 我试过做.....mapping={'Pave':1,'Lvl':2,'LandContour':3,'Utilities':4,'SaleCondition':5} #df.replace('Street',0) df.replace({'Street': 映射, 'LotShape': 映射, 'LandContour': 映射, 'Utilities': 映射, 'SaleCondition': 映射},True)。 ..但这也没有用

标签: python pandas dataframe


【解决方案1】:

试试:

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

您以前的方法的问题是它们没有将替换应用到具有正确搜索值的正确列。还要注意大小写。

【讨论】:

  • 你是生命的救星 :)
  • 乐于助人:)
猜你喜欢
  • 2021-09-07
  • 2017-09-09
  • 2017-07-08
  • 1970-01-01
  • 2022-10-13
  • 2018-09-24
  • 2018-02-11
  • 1970-01-01
相关资源
最近更新 更多