2
I have the following problem, I have a df with more than 50 columns, in these columns, some have value of type "35,57B", "6,85T". How do I make iteratively and replace the examples cited above so that they stay as follows "3557000000","6850000000000"?
I tried something like
for col in df:
df.col = (df.col.replace(r'[KMBT]+$', '', regex=True).astype(float) * df.col.str.extract(r'[\d\.]+([KMBT]+)', expand=False).fillna(1).replace(['K','M','B','T'], [10**3, 10**6, 10**9, 10**12]).astype(int))
But I was unsuccessful, and had the following mistake:
AttributeError: 'DataFrame' object has no attribute 'col'
Thanks for the answers with them it was possible to elaborate the solution. Which was the following:
for col in df.columns[1:]:
df[col] = (df[col].str.replace(',','.').replace('N/A','0').replace(r'[kMBT%]+$', '', regex=True).astype(float) * df[col].str.extract(r'[\d\.]+([kMBT%]+)', expand=False).fillna(1).replace(['k','M','B','T',"%"], [10**3, 10**6, 10**9, 10**12,1]).astype(float))
df[col] = df[col].astype(str)
df[col] = df[col].str.replace('.',',')
Can provide an example dataset?
– Lucas