Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turn "string-like" list into int with python [duplicate]

Tags:

python

pandas

After after retrieving some data, some rows of my dataframe are composed by "string-like" lists, like this one:

       bar                foo
   0     'A'                1
   1     'B'                2
   2     'B'     "['2' ,'3']"
   3     'B'              '4'
   4     'C'     "['5' ,'3']"

How could I turn the foo column into int values and take the bigger value with python pandas?

like image 994
Rodrigo Vargas Avatar asked Mar 22 '21 21:03

Rodrigo Vargas


2 Answers

literal_eval

from ast import literal_eval

df.foo.map(literal_eval).explode().max(level=0)

0    1
1    2
2    3
3    4
4    5
Name: foo, dtype: object

However, if some elements are already non-string objects

from ast import literal_eval

def l_eval(x):
    try:
        return literal_eval(x)
    except ValueError as e:
        return x

df.foo.map(l_eval).explode().max(level=0)
like image 187
piRSquared Avatar answered Oct 02 '22 10:10

piRSquared


You could use literal_eval to transform the foo column and then if the element is a list you get the max

import pandas as pd
from ast import literal_eval

...

df.foo = df.foo.map(lambda x: literal_eval(str(x)))
"""
  bar     foo
0   A       1
1   B       2
2   B  [2, 3]
3   B       4
4   C  [5, 3]
"""

df.foo = df.foo.map(lambda x: max(x) if isinstance(x, list) else x)
"""
  bar foo
0   A   1
1   B   2
2   B   3
3   B   4
4   C   5
"""
def get_max(item):
    item = literal_eval(str(item))
    return max(item) if isinstance(item, list) else item

df.foo = df.foo.map(get_max)
like image 33
Renan Lopes Avatar answered Oct 02 '22 10:10

Renan Lopes