0

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!

5
  • The error says what your problem is. Try making the lambda a variable, convert your column into a list, then pass it through. Commented Dec 3, 2020 at 20:49
  • the error makes no sense to me. lambda x is a list why would there be a type error? if i do: ``` [i for i in df[col2][#] if i >= 0] ``` this works Commented Dec 3, 2020 at 20:52
  • Is every item in column 2 a list of numbers or is there any instance in which its a single float instead of a list? Commented Dec 3, 2020 at 22:11
  • I think you're missing the point of how apply works Commented Dec 4, 2020 at 0:59
  • Look into masks and conditions Commented Dec 4, 2020 at 1:01

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Why not just df.col2[df.col2 >= 0]?
Because df.col2[df.col2 >= 0] return only >= 0 values so you can't append resulting column to existing df, , on the other hand df.col2.apply(lambda x: x if x > 0 else None) return the same length so you can append it to existing df.

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.