I want to sum values in list that store in dataframe in column path_pair
| session_id | path | path_pair |
|---|---|---|
| T01 | abc | [0.03, 0.09] |
| T02 | def | [0.02, 0.15, 0.26] |
So the result I want to acheive can be replace same column or create a new one it something looks like this:
| session_id | path | path_pair |
|---|---|---|
| T01 | abc | 0.12 |
| T02 | def | 0.43 |
How can I do the script?
CodePudding user response:
You can apply sum function to the path_pair column:
df['path_pair_sum'] = df['path_pair'].apply(sum)
Output:
session_id path path_pair path_pair_sum
0 T01 abc [0.03, 0.09] 0.12
1 T02 def [0.02, 0.15, 0.26] 0.43
CodePudding user response:
You can summarize list, and use a list comprehension to work with whole dataframe.
Here's an example:
import pandas as pd
data = {'session_id': ['T01', 'T02'], 'path_pair': [[0.03, 0.09],[0.02, 0.15, 0.26] ] }
df = pd.DataFrame(data=data)
df['sum'] = [sum(x) for x in df['path_pair']]
You will get a new column with summarized values.
CodePudding user response:
You can do it in many ways, one of the ways is the following one:
# intialise data of lists.
data = {'Name':['Tom', 'nick', 'krish', 'jack'],
'Score':[[20,12], [21,24], [19,34], [18,32]]}
# Create DataFrame
df = pd.DataFrame(data)
#Sum list in Score and replacing list with sum value
df['Score'] = [sum(x) for x in df['Score']]
# Print the output.
df
Sum is done by following code:
df['Score'] = [sum(x) for x in df['Score']]

