【问题标题】:Modifying cells in pandas df does not succeed修改 pandas df 中的单元格不成功
【发布时间】:2019-10-16 19:52:11
【问题描述】:

我正在尝试修改现有 df 中的单元格——如果我发现没有字母字符的字符串(例如 "*" ),我将其设置为 "0.0" 字符串,并且当所有单元格都被处理后,我尝试转换一列数字类型。 但是由于某种原因设置“0.0”并没有反映在结果df中

for i, col in enumerate(cols):
    for ii in range(0, df.shape[0]):
        row = df.iloc[ii]
        value = row[col]

        if isinstance(value, str):
            if not( utils.representsInt(value) or utils.representsFloat(value) ) and re.search('[a-zA-Z]', x) is None:
                df.iat[ii, i] = "0.0"

     df[col] = df[col].astype(np.float_)
    #df[col] = df[col].to_numeric() #this throws error that Series does not have to_numeric()

我收到错误

could not convert string to float: 'cat'

当我打印 df 时,我看到值没有改变。 可能是什么问题?

谢谢!

df

f289,f290,f291,f292,f293,f294,f295,f296,f297,f298,f299,f300,f301,f302,f303,f304,f305,f306,f307,f308,f309,f310
01M015,P.S. 015 Roberto Clemente,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M019,P.S. 019 Asher Levy,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M020,P.S. 020 Anna Silver,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M034,P.S. 034 Franklin D. Roosevelt,K-8,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,14
01M063,The STAR Academy - P.S.63,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,6
01M064,P.S. 064 Robert Simon,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M110,P.S. 110 Florence Nightingale,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M134,P.S. 134 Henrietta Szold,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M137,P.S. 137 John L. Bernstein,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M140,P.S. 140 Nathan Straus,K-8,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M142,P.S. 142 Amalia Castro,Elementary,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M184,P.S. 184m Shuang Wen,K-8,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
01M188,P.S. 188 The Island School,K-8,1.0,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,10

因此,在这种情况下,我希望 df 具有“0.0”而不是“*”,并且这些列在转换后具有数字数据类型,例如 float

【问题讨论】:

  • 相当嵌套的 for 循环,您可以发布您的示例数据以便我们帮助您查询吗?至于为什么它不起作用是因为 1. 你的浮点数中有字符串 2. 你的调用 .to_numeric 错误尝试 pd.to_numeric(df[col],errors='coerce')
  • df.applymap(lambda x: 0.0 if isinstance(x, str) and (not( utils.representsInt(x) or utils.representsFloat(x))) else x)))怎么样
  • @splash58 更新帖子
  • @Datanovice 更新帖子

标签: python pandas


【解决方案1】:

你可以改变返回0.0的条件,我为测试设置x=="*"

df.iloc[:,3:] = df.iloc[:,3:].applymap(lambda x: 0.0 if x=="*" else x)

      f289                            f290        f291  ...  f308  f309  f310
0   01M015       P.S. 015 Roberto Clemente  Elementary  ...   0.0   0.0     0
1   01M019             P.S. 019 Asher Levy  Elementary  ...   0.0   0.0     0
2   01M020            P.S. 020 Anna Silver  Elementary  ...   0.0   0.0     0
3   01M034  P.S. 034 Franklin D. Roosevelt         K-8  ...   0.0   0.0    14
4   01M063       The STAR Academy - P.S.63  Elementary  ...   0.0   0.0     6
5   01M064           P.S. 064 Robert Simon  Elementary  ...   0.0   0.0     0
6   01M110   P.S. 110 Florence Nightingale  Elementary  ...   0.0   0.0     0
7   01M134        P.S. 134 Henrietta Szold  Elementary  ...   0.0   0.0     0
8   01M137      P.S. 137 John L. Bernstein  Elementary  ...   0.0   0.0     0
9   01M140          P.S. 140 Nathan Straus         K-8  ...   0.0   0.0     0
10  01M142          P.S. 142 Amalia Castro  Elementary  ...   0.0   0.0     0
11  01M184            P.S. 184m Shuang Wen         K-8  ...   0.0   0.0     0
12  01M188      P.S. 188 The Island School         K-8  ...   0.0   0.0    10

更新

定义函数

def f(value) :
   if isinstance(value, str):
      if not(utils.representsInt(value) or utils.representsFloat(value) ) and re.search('[a-zA-Z]', x) is None:
      return 0.0
   return float(value)

将其应用于每个单元格

df = df.applymap(f)

【讨论】:

  • 这应该是自动化的,无需编码。数据框只是一个例子。我有成千上万个不同的表需要这种处理,“*”可以替换为任何其他非字母字符,需要处理的列可以任意定位
  • 这就是我使用提供的函数的原因
  • 好的。您可以不应用于切片,而是应用于所有帧 df.apply。而不是测试 lambda 而是真正的功能
猜你喜欢
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
相关资源
最近更新 更多