I’ve been able to get this to execute once but since then the kernel just hangs and then dies. Any help would be appreciated.
[Dataframe head]
new_companies_df['Total Raised']= new_companies_df['Total Raised'].str.replace('$','') #Program ref source:https://www.geeksforgeeks.org/python-convert-suffix-denomination-to-values/ #Initialize values dictionary val_dict = {"M": 1000000, "B": 100000000,'K':1000,'None': 0} result = [] for x in new_companies_df['Total Raised']: for key in val_dict: if key in value: # conversion of dictionary keys to values val = (x.replace(key, "")) * val_dict[key] result.append(val) new_companies_df['Total Raised ($)'] = result new_companies_df.head()
Advertisement
Answer
You might want to consider using .apply()
function from pandas directly with a custom function or a lambda expression. Something like this
def replace(x): val_dict = {"M": 1000000, "B": 100000000,'K':1000,'None': 0} for key in val_dict: if key in x: value = float(x.replace(key, "")) # converting to float is important multiplier = float(val_dict[key]) return value * multiplier new_companies_df['Total Raised ($)'] = new_companies_df['Total Raised'].apply(replace)
I do not see any conversion to float. I suspect that your kernel died because you are multiplying strings with a large number which can get very nasty results. For example
"1" * 10 # this produces -> "1111111111" 1 * 10 # this produces -> 10