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]
JavaScript
x
22
22
1
new_companies_df['Total Raised']= new_companies_df['Total Raised'].str.replace('$','')
2
3
#Program ref source:https://www.geeksforgeeks.org/python-convert-suffix-denomination-to-values/
4
5
#Initialize values dictionary
6
val_dict = {"M": 1000000, "B": 100000000,'K':1000,'None': 0}
7
8
result = []
9
10
for x in new_companies_df['Total Raised']:
11
12
for key in val_dict:
13
if key in value:
14
15
# conversion of dictionary keys to values
16
val = (x.replace(key, "")) * val_dict[key]
17
result.append(val)
18
19
new_companies_df['Total Raised ($)'] = result
20
21
new_companies_df.head()
22
Advertisement
Answer
You might want to consider using .apply()
function from pandas directly with a custom function or a lambda expression. Something like this
JavaScript
1
11
11
1
def replace(x):
2
val_dict = {"M": 1000000, "B": 100000000,'K':1000,'None': 0}
3
4
for key in val_dict:
5
if key in x:
6
value = float(x.replace(key, "")) # converting to float is important
7
multiplier = float(val_dict[key])
8
return value * multiplier
9
10
new_companies_df['Total Raised ($)'] = new_companies_df['Total Raised'].apply(replace)
11
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
JavaScript
1
3
1
"1" * 10 # this produces -> "1111111111"
2
1 * 10 # this produces -> 10
3