【问题标题】:Converting string variable with double commas into float?将带有双逗号的字符串变量转换为浮点数?
【发布时间】:2022-06-15 21:53:00
【问题描述】:

我在一列中有一些字符串,最初使用逗号作为千位和小数的分隔符,我需要将此字符串转换为浮点数,我该怎么做?

我首先尝试将所有逗号替换为点:

df['min'] = df['min'].str.replace(',', '.')

并尝试转换为浮点数:

df['min']= df['min'].astype(float) 

但它返回了以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-29-5716d326493c> in <module>
----> 1 df['min']= df['min'].astype(float)
      2 #df['mcom']= df['mcom'].astype(float)
      3 #df['max']= df['max'].astype(float)

~\anaconda3\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors)
   5544         else:
   5545             # else, only a single dtype is given
-> 5546             new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
   5547             return self._constructor(new_data).__finalize__(self, method="astype")
   5548 

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in astype(self, dtype, copy, errors)
    593         self, dtype, copy: bool = False, errors: str = "raise"
    594     ) -> "BlockManager":
--> 595         return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
    596 
    597     def convert(

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, align_keys, **kwargs)
    404                 applied = b.apply(f, **kwargs)
    405             else:
--> 406                 applied = getattr(b, f)(**kwargs)
    407             result_blocks = _extend_blocks(applied, result_blocks)
    408 

~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors)
    593             vals1d = values.ravel()
    594             try:
--> 595                 values = astype_nansafe(vals1d, dtype, copy=True)
    596             except (ValueError, TypeError):
    597                 # e.g. astype_nansafe can fail on object-dtype of strings

~\anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
    993     if copy or is_object_dtype(arr) or is_object_dtype(dtype):
    994         # Explicit copy, or required since NumPy can't view from / to object.
--> 995         return arr.astype(dtype, copy=True)
    996 
    997     return arr.view(dtype)

ValueError: could not convert string to float: '1.199.75'

如果可能,我想删除所有点和逗号,然后在转换为浮点数之前在变量的最后两个字符之前添加点。

输入:

df['min'].head()
9.50
10.00
3.45
1.095.50
13.25

预期输出:

9.50
10.00
3.45
1095.50
13.25

【问题讨论】:

  • 所以要删除所有点并在两个字符前添加点?
  • df['min'].str.replace('.', '').str.replace(',', '.')?
  • @DemetreDzmanashvili 是的
  • 您能否添加示例输入和预期输出以帮助回答
  • @mozway 数据框最初有逗号作为千位和小数的分隔符,此命令不起作用

标签: python pandas dataframe numpy


【解决方案1】:

如果你总是有 2 位小数:

df['min'] = pd.to_numeric(df['min'].str.replace('.', '', regex=False)).div(100)

输出(为清楚起见作为新列 min2):

        min     min2
0      9.50     9.50
1     10.00    10.00
2      3.45     3.45
3  1.095.50  1095.50
4     13.25    13.25

【讨论】:

    【解决方案2】:

    试试这个。

    df['min'] = df['min'].str.replace(',', '')
    df['min'] = df['min'].str[:-2] + '.' + df['min'].str[-2:]
    
    df['min']= df['min'].astype(float) 
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      我在一列中有一些字符串,最初使用 逗号作为千位分隔符小数点,我需要将此字符串转换为浮点数

      所以让我们生成一个符合您描述的可重现数据源:

      df = {'min': '0123,456,78'}
      

      然后将“,”上的这个拆分成一个列表:

      split_str = df['min'].split(',')
      

      分别收集整数和小数部分:

      int_str=''.join(split_str[:-1])
      dec_str = split_str[-1]
      

      最后重构一个有效的浮点字符串;并将其转换为实际的浮点数:

      float_number = float(f"{int_str}.{dec_str}")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多