Reputation: 189
I have columns that are values 2/300/3
like this, but I want some of this value, in this case, 2+300+3=305
. In order to achieve this goal I am using the command
for i in range(len(df.col)):
df.col.iloc[i]=sum([float(j) for j in df.col.iloc[i].split("|")])
The length of this data frame is big, that why the above command is very time-consuming. Does there exist another way to do the same operation without loop? NOTE: the number of "/" is not fixed and in some cases it does not exist
Upvotes: 0
Views: 129
Reputation: 2086
replace
and eval
can do the job
import pandas as pd
data = ["2/300/3" , "5/300/3" , "3 50 3"]
df =pd.DataFrame(data, columns=["data"])
df["total_sum"] = df["data"].str.replace('[/ |" "]','+').apply(pd.eval)
Output:
data total_sum
0 2/300/3 305
1 5/300/3 308
2 3 50 3 56
Upvotes: 1
Reputation: 14949
you can also use str.split and apply
import pandas as pd
data = ["2/300/3" , "5/300/3" , "3/50/3"]
df =pd.DataFrame(data, columns=["data"])
df.data = df.data.str.split('/').apply(lambda x: sum(map(int,x)))
Upvotes: 0