【问题标题】:Convert comma separator objects to numeric in Pandas在 Pandas 中将逗号分隔符对象转换为数字
【发布时间】:2021-06-20 14:14:07
【问题描述】:

我有一个数据类型为objectint 的表。

其中之一是带有美元符号 ($) 和逗号分隔符的美元金额。我想使用describe() 来总结数据框,所以我尝试通过考虑$ 符号来读取文件,然后将对象转换为整数:

df= pd.read_excel(r'C:\Users\xxxx\df.xlsx','my_df' ,engine="openpyxl", thousands=',')
df['my_col'] = df['my_col'].replace({'\$':''}, regex = True)
df['my_col'].astype(str).astype(int)
df.describe(datetime_is_numeric=True)

但它发现了错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-133-2011d1ad889e> in <module>
      4 
      5 df['my_col'] = df['my_col'].replace({'\$':''}, regex = True)
----> 6 df['my_col'].astype(str).astype(int)
      7 df.describe(datetime_is_numeric=True)

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors)
   5535         else:
   5536             # else, only a single dtype is given
-> 5537             new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
   5538             return self._constructor(new_data).__finalize__(self, method="astype")
   5539 

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\managers.py in astype(self, dtype, copy, errors)
    565         self, dtype, copy: bool = False, errors: str = "raise"
    566     ) -> "BlockManager":
--> 567         return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
    568 
    569     def convert(

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\managers.py in apply(self, f, align_keys, **kwargs)
    394                 applied = b.apply(f, **kwargs)
    395             else:
--> 396                 applied = getattr(b, f)(**kwargs)
    397             result_blocks = _extend_blocks(applied, result_blocks)
    398 

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors)
    588             vals1d = values.ravel()
    589             try:
--> 590                 values = astype_nansafe(vals1d, dtype, copy=True)
    591             except (ValueError, TypeError):
    592                 # e.g. astype_nansafe can fail on object-dtype of strings

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
    964         # work around NumPy brokenness, #1987
    965         if np.issubdtype(dtype.type, np.integer):
--> 966             return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
    967 
    968         # if we have a datetime/timedelta array of objects

pandas\_libs\lib.pyx in pandas._libs.lib.astype_intsafe()

ValueError: invalid literal for int() with base 10: '500.00'

如果我将df['my_col'].astype(str).astype(int) 更改为df['my_col'].astype(str).astype(float),它将捕获错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-134-65da7cbc042f> in <module>

      4 
      5 df['my_col'] = df['my_col'].replace({'\$':''}, regex = True)
----> 6 df['my_col'].astype(str).astype(int)
      7 df.describe(datetime_is_numeric=True)

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors)
   5535         else:
   5536             # else, only a single dtype is given
-> 5537             new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
   5538             return self._constructor(new_data).__finalize__(self, method="astype")
   5539 

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\managers.py in astype(self, dtype, copy, errors)
    565         self, dtype, copy: bool = False, errors: str = "raise"
    566     ) -> "BlockManager":
--> 567         return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
    568 
    569     def convert(

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\managers.py in apply(self, f, align_keys, **kwargs)
    394                 applied = b.apply(f, **kwargs)
    395             else:
--> 396                 applied = getattr(b, f)(**kwargs)
    397             result_blocks = _extend_blocks(applied, result_blocks)
    398 

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors)
    588             vals1d = values.ravel()
    589             try:
--> 590                 values = astype_nansafe(vals1d, dtype, copy=True)
    591             except (ValueError, TypeError):
    592                 # e.g. astype_nansafe can fail on object-dtype of strings

~\AppData\Roaming\Python\Python38\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
    987     if copy or is_object_dtype(arr) or is_object_dtype(dtype):
    988         # Explicit copy, or required since NumPy can't view from / to object.
--> 989         return arr.astype(dtype, copy=True)
    990 
    991     return arr.view(dtype)

ValueError: could not convert string to float: '5,000.00'

【问题讨论】:

  • 嘿,谢谢@AnuragDabas 它似乎运行了,但df.dtypes 显示该列的数据类型仍然是object
  • 也试试这个pd.to_numeric(df['my_col'].astype(str).str.replace(',',''),errors='coerce')
  • @AnuragDabas df.dtypes 仍然显示object,我尝试使用df[df['my_col']&gt;1000] 进行切片,但它返回TypeError: '&gt;' not supported between instances of 'str' and 'int',现在我很困惑该列是str 还是@987654338 @ 格式。我是否按照您的意思处理了您的解决方案?

标签: python pandas dataframe object data-structures


【解决方案1】:

更改replace 增加一个条件

df['my_col'] = df['my_col'].replace({'\$':'',',':''}, regex = True)

【讨论】:

  • 您好 BENY,感谢您的回答。它似乎运行但 df.dtypes 显示该列的数据类型仍然是对象:/
  • @nilsinelabore 在此之后您可以通过 pd.to_numeric 进行转换
  • 谢谢 BENY,你知道为什么 df['my_col'] = pd.to_numeric(df['my_col'].astype(str).str.replace(',',''),errors='coerce') 允许我按 df[df['my_col'] &gt; 1000000] 切片数据并返回结果,但不能返回空表的 df['my_col'] = df.apply(pd.to_numeric, errors='coerce') 吗?但是df.dtypes 显示该列的数据类型对于这两种情况都是float64
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多