0

I try to use panda df[].astype(float).describe But, there is a problem. My data type is like$8,120,000,000 So, how to convert to 8120000000? it shows

--------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) <ipython-input-177-5ee173ce9990> in <module>()
----> 1 df['Revenue'].astype(float).describe()

4 frames /usr/local/lib/python3.6/dist-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
    988     if copy or is_object_dtype(arr) or is_object_dtype(dtype):
    989         # Explicit copy, or required since NumPy can't view from / to object.
--> 990         return arr.astype(dtype, copy=True)
    991 
    992     return arr.view(dtype)

ValueError: could not convert string to float: '$8,120,000,000'

I tried to use replace to change the list

revenue_list = [item.replace("$", "") for item in revenue_list]
revenue_list = [item.replace(",", "") for item in revenue_list]
print(revenue_list)

But, it still doesn't work

Rakesh
  • 81,458
  • 17
  • 76
  • 113
Shawn Zeng
  • 11
  • 4
  • How was the dataframe created? – Trenton McKinney Oct 09 '20 at 05:53
  • 1
    `df = pd.read_csv('test4.csv', converters={'Revenue': lambda s: s.replace('$', '').replace(',', '')})` the parameter `thousands=','` doesn't work with the converter, so replace both `$` and `,` when reading the file. – Trenton McKinney Oct 09 '20 at 05:56
  • 2
    `df['Revenue'] = df['Revenue'].replace({'\$': '', ',': ''}, regex=True).astype(float)`, your `$` in replace need an escape character "\" for it to work `[item.replace("\$", "") for item in revenue_list]` – Shijith Oct 09 '20 at 05:59
  • I got it. Do not replace after read the file, do it when read it. It works now, it shows numbers. – Shawn Zeng Oct 09 '20 at 06:35

0 Answers0