I have a CSV file that has a column with values like €118.5M or €60K, and I need to convert these into integers or floats and then do calculations.
import pandas as pd
file = pd.read_csv("data.csv",index_col ="ID")
file[:10]
print(file)
€118.5M
....
€60K
Damn, a quick google search finds:
def convert_si_to_number(x):
total_stars = 0
x = x.replace('€', '')
if 'k' in x:
if len(x) > 1:
total_stars = float(x.replace('k', '')) * 1000 # convert k to a thousand
elif 'M' in x:
if len(x) > 1:
total_stars = float(x.replace('M', '')) * 1000000 # convert M to a million
elif 'B' in x:
total_stars = float(x.replace('B', '')) * 1000000000 # convert B to a Billion
else:
total_stars = int(x) # Less than 1000
return int(total_stars)
An easy way to apply to a df is run it on a loop
for i, stringo in enumerate(file):
file[i] = convert_si_to_number(stringo)