Reison
Reison

Reputation: 143

lambda function on list in dataframe column error

I have a list of numbers inside of a pandas dataframe and i am trying to use a lambda function + list comprehension to remove values from these lists.

col 1 col2

a [-1, 2, 10, 600, -10]

b [-0, -5, -6, -200, -30]

c .....

etc.

df.col2.apply(lambda x: [i for i in x if i>= 0]) #just trying to remove negative values 

numbers are always ascending and can be all negative, all positive or a mix. lists are about 200 items long all integers.

I get this error:

TypeError: 'numpy.float64' object is not iterable

Edit: when i do it this way it works [i for i in df[col2][#] if i >= 0] i guess i could run this through a for loop.. seems slow though

Edit2: looking at it with fresh eyes. turns out that the column isn't entirely made up of lists there are a few float values spread throughout (duh). Something weird was going on with the merge, once i corrected that the code above worked as expected. Thanks for the help!

Upvotes: 0

Views: 1891

Answers (1)

Amir saleem
Amir saleem

Reputation: 1496

Because x in your lambda is a float number, and you cant loop over float :p. if you need to do so. you can

In [2]: np.random.seed(4)
   ...: df = pd.DataFrame(np.random.randint(-5,5, 7)).rename(columns={0:"col2"})
   ...: df.col2 = df.col2.astype(float)
   ...: df
Out[2]: 
   col2
0   2.0
1   0.0
2  -4.0
3   3.0
4   2.0
5   3.0
6  -3.0

In [3]: df.col2.apply(lambda x: x if x > 0 else None).dropna()
Out[3]: 
0    2.0
3    3.0
4    2.0
5    3.0
Name: col2, dtype: float64

Upvotes: 1

Related Questions