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
?
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With