在python中创建虚拟对象时替换列值中的$

2024-05-14 09:06:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据,其中列值包含$。这使得创建的虚拟对象是dtype unsigned integer。我正在尝试用“”替换“$”,以解决数据类型问题。你知道吗

输入

grp_m1      grp_m2      grp_m3      grp_m4
$50-$75     $50-$75     $50-$75     $50-$75
$50-$75     $50-$75     $50-$75     $50-$75
$150-$175   $150-$175   $150-$175   $150-$175
$100-$125   $100-$125   $100-$125   $100-$125
$150-$175   $125-$150   $125-$150   $125-$150

用“”替换“$”的代码:

if ('$' in str(df_column[column]) == True):
  new_val = [x.strip('$') for x in df_column[column].astype('str')]
  df_column.replace(column.values,values = new_val, inplace = True)

我现在正在尝试为一个列执行,理想情况下,我希望处理包含“$”的所有列。所以df\u列是数据框,列是一个包含列名称grp\u m1---grp\u m4的列表。你知道吗

结果:

没有抛出错误,但列值没有更改。你知道吗

预期输出:

grp_m1      grp_m2      grp_m3      grp_m4
50-75     50-75     50-75     50-75
50-75     50-75     50-75     50-75
150-175   150-175   150-175   150-175
100-125   100-125   100-125   100-125
150-175   125-150   125-150   125-150           

有人能帮我吗?你知道吗

我使用了提供的解决方案:

if ('$' in str(df_column[column]) == True):
   df_column.values.replace('\$', '',inplace = True, regex=True)

但是数据帧中的值没有变化。你知道吗


Tags: 数据intruedfnewifcolumnval

热门问题