user1709708
user1709708

Reputation: 1577

How to filter numeric string list in dataframe column?

d = {'col1': "{35.1, 43.76, 41.5, 38.71}", 'col2': [3, 4]}
df = pd.DataFrame(data=d)

Assuming I don't have direct access to d (and d may contain more rows than shown), I would like to filter the values encoded in col1 so that all numeric values lie within a specified interval. Lets say the interval is [40, 45] then the expected result would be:

{'col1': "{43.76, 41.5}", 'col2': [3, 4]}

Is that somehow possible in an elegant pythonic way?

Upvotes: 0

Views: 89

Answers (2)

Andy L.
Andy L.

Reputation: 25259

Using literal_val to convert to set and map to convert and filter on condition as follows:

import ast

df['col1'] = df.col1.map(lambda x: {item for item in ast.literal_eval(x) if (40<=item <=45)})


Out[1734]:
            col1  col2
0  {43.76, 41.5}     3
1  {43.76, 41.5}     4

Upvotes: 0

piRSquared
piRSquared

Reputation: 294358

literal_eval

from ast import literal_eval

pred = lambda x: 40 <= x <= 45
lamb = lambda s: str({*filter(pred, literal_eval(s))})
df.assign(col1=df.col1.apply(lamb))

            col1  col2
0  {41.5, 43.76}     3
1  {41.5, 43.76}     4

Upvotes: 2

Related Questions