1

I have a df like this"

col1
['525', '2830', '43', '567']    
['432', '324', '3993', '5675']  
['234', '2830', '342312', '4410']   

df.applymap(type) shows all rows in this column are <class 'str'>:

I'm trying to convert the elements in this df.col to integers and sum them in a new column. I've tried like a billion things but have been unsuccessful.

#attempts
#converted to list
map(int, list()) 
map(int, list().split()) 

#geeks for geeks suggestions:
for i in range(0, len(list)): 
    list[i] = int(list[i]) 

test_list = [int(i) for i in test_list] 

I just get this error constantly:

ValueError: invalid literal for int() with base 10: '[525 2830 3993 4410]'

Any ideas?

2 Answers 2

1
df['sum'] = df['col1'].apply(lambda x: sum(int(v) for v in x))
print(df)

Prints:

                        col1     sum
0       [525, 2830, 43, 567]    3965
1     [432, 324, 3993, 5675]   10424
2  [234, 2830, 342312, 4410]  349786

EDIT:

import ast

df['sum'] = df['col1'].apply(lambda x: sum(int(v) for v in ast.literal_eval(x)))
print(df)
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting this error: ValueError: invalid literal for int() with base 10: '['??
@max See my edit. It seems that you store strings instead of lists in col1.
one more question - what if i wanted to add a column that counts the number of items in each of these lists? 4, 4, 4 for the example above
@max You can do df["len"] = df["col1"].apply(lambda x: len(ast.literal_eval(x)))
1

Try this

import ast
df.col1.map(lambda x: sum(map(int, ast.literal_eval(x))))

Out[789]:
0      3965
1     10424
2    349786
Name: col1, dtype: int64

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.