Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If Then statement
#1
I hope you are all having a good day. I want to know why the following code does not work:

for X in df3['diff']:
    if X < 0:
        X=NaN
Reply
#2
What is in df3['diff']. It must contain an iterable.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
1) df3 isn't defined, so you can't iterate over nothing.
2) NaN isn't defined, so you can't set X to it.
3) Even if both of those did exist, setting the temporary variable X to whatever NaN is wouldn't persist that change outside of the for-loop.

>>> items = [2, 5, 2, 4, 3, -32, 4]
>>> for item in items:
...   if item < 0:
...     item = None
...
>>> items
[2, 5, 2, 4, 3, -32, 4]
Reply
#4
I would guess that df3 is a dataframe with column 'diff' with int or float dtype. In that case, as nilamo pointed out with 3), its similar to iterating over list of numbers and your "changes" do not propagate into your dataframe column.

Iterating over serie/dataframe is only rarely needed, using subsetting(indexing) or .apply() is preferred (if possible). You can use subsetting with condition and assign new value directly:
df3['diff'][df3['diff'] < 0] = NaN   # I suppose that you did "from numpy import *"
Reply
#5
(Apr-24-2017, 04:15 PM)zivoni Wrote:
df3.diff[df3.diff < 0] = NaN   # I suppose that you did "from numpy import *"

That's actually pretty cool.  I should really look into numpy eventually.  Overloading property accessors and indexing feels weird, but looks powerful.
Reply
#6
smw10c why do not tell that this is about Pandas.
Next time post code that has meaning or is possible to run,
or your post will be deleted.
Reply
#7
(Apr-24-2017, 04:19 PM)snippsat Wrote: smw10c why do not tell that this is about Pandas.
Next time post code that has meaning or is possible to run,
or your post will be deleted.

I did not know this was about Pandas. I'm a new user which I thought was the point of this forum.
Reply
#8
(Apr-24-2017, 04:18 PM)nilamo Wrote: That's actually pretty cool.  I should really look into numpy eventually.  Overloading property accessors and indexing feels weird, but looks powerful.

Yes, sadly I forgot that dataframe has .diff(), so I had to edit my post. Now it looks less cool ...
Reply
#9
(Apr-24-2017, 04:15 PM)zivoni Wrote: I would guess that df3 is a dataframe with column 'diff' with int or float dtype. In that case, as nilamo pointed out with 3), its similar to iterating over list of numbers and your "changes" do not propagate into your dataframe column.

Iterating over serie/dataframe is only rarely needed, using subsetting(indexing) or .apply() is preferred (if possible). You can use subsetting with condition and assign new value directly:
df3['diff'][df3['diff'] < 0] = NaN   # I suppose that you did "from numpy import *"

Why do they not propagate into my data frame column?

(Apr-24-2017, 04:18 PM)nilamo Wrote:
(Apr-24-2017, 04:15 PM)zivoni Wrote:
df3.diff[df3.diff < 0] = NaN   # I suppose that you did "from numpy import *"

That's actually pretty cool.  I should really look into numpy eventually.  Overloading property accessors and indexing feels weird, but looks powerful.

Additionally, the code above did not work. It states that "name NaN is not defined".
Reply
#10
(Apr-24-2017, 04:30 PM)smw10c Wrote: Additionally, the code above did not work. It states that "name NaN is not defined".

You should probably share your entire code then, instead of a small snippet which we all knew wouldn't work :P
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020