Python Forum
If Then statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: If Then statement (/thread-3012.html)

Pages: 1 2


If Then statement - smw10c - Apr-24-2017

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


RE: If Then statement - wavic - Apr-24-2017

What is in df3['diff']. It must contain an iterable.


RE: If Then statement - nilamo - Apr-24-2017

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]



RE: If Then statement - zivoni - Apr-24-2017

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 *"



RE: If Then statement - nilamo - Apr-24-2017

(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.


RE: If Then statement - snippsat - Apr-24-2017

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.


RE: If Then statement - smw10c - Apr-24-2017

(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.


RE: If Then statement - zivoni - Apr-24-2017

(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 ...


RE: If Then statement - smw10c - Apr-24-2017

(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".


RE: If Then statement - nilamo - Apr-24-2017

(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