'How I replace a string in a list that contain int and float? in python
I am trying to clean a database. I am extracting each column and cleaning separately with this formula:
def Cleaning_strings(Data,Correction):
for i in range(len(Data)):
if isinstance(Data[i], float) :
Mom_Normal = Data[i]
Correction.append(Mom_Normal)
elif isinstance(Data[i], int):
Mom_Normal1 = Data[i]
Correction.append(Mom_Normal1)
else:
Zeros = int(Data[i].replace('--','0'))
Correction.append(Zeros)
For example, if i have a series (which I extracted before like this : df['Name of the column']) like this: Data = [4.0,5,3.4,'--'] the code work perfectly but if I have the data like this Data =[4.0,5,3.4,'--','1.45k'] it does not work.
I'm trying to change the code like this:
def Cleaning_strings(Data,Correction):
for i in range(len(Data)):
if isinstance(Data[i], float) : #Como cada dato se considera
Mom_Normal = Data[i]
Correction.append(Mom_Normal)
elif isinstance(Data[i], int):
Mom_Normal1 = Data[i]
Correction.append(Mom_Normal1)
elif isinstance(Data[i], str):
Thousands = int(Data[i].replace('k',''))*1000
Correction.append(Thousands)
else:
Zeros = int(Data[i].replace('--','0')) #No hay dato
Correction.append(Zeros)
and the error that appears is:
ValueError: invalid literal for int() with base 10: '--'
I understand why I get this error, but what I don't know is how I can modify the formula to make it work on this data. Please help me to find the way to replace the 'k' of the data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
