at present, I am building a consolidated financial statement system, which grabs numbers from the financial system and then does data cleaning and calculation, in which the cleaning phase is mainly completed by pandas. In the captured data, the numbers are separated by a thousand quantile, and when importing pandas, it is recognized as the object type rather than float, which affects the subsequent calculation, so type conversion is needed. The conversion code is as follows, according to which an error is reported:
temp_file = pd.read_csv(file_name, index_col=None, low_memory=False)
col_float = ["", "", "", "", "", ""]
for c in col_float:
if isinstance(temp_file[c], object):
temp_file[c] = temp_file[c].str.replace(",", "")
temp_file[c].fillna(0, inplace=True)
temp_file[c] = temp_file[c].astype(float)
prompts the following error when executing code:
Traceback (most recent call last):
File "E:/Flask/Consol_demo/test consol.py", line 12, in <module>
df = rf.pl_clean(r"e:\test files\tables\1801PLCZ.csv")
File "E:\Flask\Consol_demo\consol\apps\data_washer.py", line 51, in pl_clean
temp_file[c] = temp_file[c].str.replace(",", "")
File "E:\Flask\Consol_demo\venv\lib\site-packages\pandas\core\generic.py", line 4372, in __getattr__
return object.__getattribute__(self, name)
File "E:\Flask\Consol_demo\venv\lib\site-packages\pandas\core\accessor.py", line 133, in __get__
accessor_obj = self._accessor(obj)
File "E:\Flask\Consol_demo\venv\lib\site-packages\pandas\core\strings.py", line 1895, in __init__
self._validate(data)
File "E:\Flask\Consol_demo\venv\lib\site-packages\pandas\core\strings.py", line 1917, in _validate
raise AttributeError("Can only use .str accessor with string "
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in panda
the error points to this line of code:
if isinstance(temp_file[c], object):
temp_file[c] = temp_file[c].str.replace(",", "")
ask all the great gods to answer how to solve this problem, thank you!