##### If Then statement
 If Then statement smw10c Silly Frenchman Posts: 35 Threads: 13 Joined: Mar 2017 Reputation: 0 Apr-24-2017, 04:41 PM (This post was last modified: Apr-24-2017, 04:42 PM by smw10c.) (Apr-24-2017, 04:39 PM)nilamo Wrote: (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 Haha okay! dict3={'initial cost': ['\$75000.00','\$0.00','\$30000.00','\$1500.00','\$19500.00'],'Total Est. Fee':['\$986.00','\$1144.00','\$522.50', '\$225.00', '\$389.50']} df3=pd.DataFrame(dict3) df3['Total Est. Fee']=df3['Total Est. Fee'].str.split('\$') df3['Total Est. Fee']=df3['Total Est. Fee'].str.get(1) df3['initial cost']=df3['initial cost'].str.split('\$') df3['initial cost']=df3['initial cost'].str.get(1) df3['Total Est. Fee']=pd.to_numeric(df3['Total Est. Fee'],errors='coerce') df3['diff']=df3['initial cost']-df3['Total Est. Fee'] df3.diff[df3.diff < 0] = NaN For some reason the second to last line of code isn't running anymore even though it ran earlier... Reply zivoni Minister of Silly Walks Posts: 331 Threads: 2 Joined: Feb 2017 Reputation: 65 Apr-24-2017, 04:47 PM Try to run/think over nilamo's point 3 - its same for your code. In body of your loop you set `X` to reference "cell" content, and after that you rebind `X` to a new value (NaN) - but you change just `X`, not dataframe cell. Its similar to ```item =  x = item x = 10``` - if you check item list, it would be still `` - unchanged. Reply smw10c Silly Frenchman Posts: 35 Threads: 13 Joined: Mar 2017 Reputation: 0 Apr-24-2017, 04:55 PM (Apr-24-2017, 04:47 PM)zivoni Wrote: Try to run/think over nilamo's point 3 - its same for your code. In body of your loop you set `X` to reference "cell" content, and after that you rebind `X` to a new value (NaN) - but you change just `X`, not dataframe cell. Its similar to ```item =  x = item x = 10``` - if you check item list, it would be still `` - unchanged. I understand what you are saying. I just don't get how I'd fix this in the loop above. Thank you for the further explanation. I would really appreciate if you showed me how to do it in the way I was trying so I can better understand Python logic. Reply zivoni Minister of Silly Walks Posts: 331 Threads: 2 Joined: Feb 2017 Reputation: 65 Apr-24-2017, 05:08 PM (Apr-24-2017, 04:30 PM)smw10c Wrote: Additionally, the code above did not work. It states that "name NaN is not defined".You have used `NaN` in your code - that was reason why i supposed that you imported it from numpy. (Apr-24-2017, 04:41 PM)smw10cp Wrote: ```dict3={'initial cost': ['\$75000.00','\$0.00','\$30000.00','\$1500.00','\$19500.00'],'Total Est. Fee':['\$986.00','\$1144.00','\$522.50', '\$225.00', '\$389.50']} df3=pd.DataFrame(dict3) df3['Total Est. Fee']=df3['Total Est. Fee'].str.split('\$') df3['Total Est. Fee']=df3['Total Est. Fee'].str.get(1) df3['initial cost']=df3['initial cost'].str.split('\$') df3['initial cost']=df3['initial cost'].str.get(1) df3['Total Est. Fee']=pd.to_numeric(df3['Total Est. Fee'],errors='coerce') df3['diff']=df3['initial cost']-df3['Total Est. Fee'] df3.diff[df3.diff < 0] = NaN```For some reason the second to last line of code isn't running anymore even though it ran earlier... You didnt convert `df3['initial cost']` to a number. Morever you should modify last line to `df3['diff'][df3['diff'] < 0] = NaN`, as i did in my post (initially i forgot that .diff() is a dataframe method). That code need imports for pandas and numpy - atleast ```import pandas as pd from numpy import NaN``` likely you didnt run imports in your second run and that was why it crashed on second line. Reply smw10c Silly Frenchman Posts: 35 Threads: 13 Joined: Mar 2017 Reputation: 0 Apr-24-2017, 05:17 PM (Apr-24-2017, 05:08 PM)zivoni Wrote: (Apr-24-2017, 04:30 PM)smw10c Wrote: Additionally, the code above did not work. It states that "name NaN is not defined".You have used `NaN` in your code - that was reason why i supposed that you imported it from numpy. (Apr-24-2017, 04:41 PM)smw10cp Wrote: ```dict3={'initial cost': ['\$75000.00','\$0.00','\$30000.00','\$1500.00','\$19500.00'],'Total Est. Fee':['\$986.00','\$1144.00','\$522.50', '\$225.00', '\$389.50']} df3=pd.DataFrame(dict3) df3['Total Est. Fee']=df3['Total Est. Fee'].str.split('\$') df3['Total Est. Fee']=df3['Total Est. Fee'].str.get(1) df3['initial cost']=df3['initial cost'].str.split('\$') df3['initial cost']=df3['initial cost'].str.get(1) df3['Total Est. Fee']=pd.to_numeric(df3['Total Est. Fee'],errors='coerce') df3['diff']=df3['initial cost']-df3['Total Est. Fee'] df3.diff[df3.diff < 0] = NaN```For some reason the second to last line of code isn't running anymore even though it ran earlier... You didnt convert `df3['initial cost']` to a number. Morever you should modify last line to `df3['diff'][df3['diff'] < 0] = NaN`, as i did in my post (initially i forgot that .diff() is a dataframe method). That code need imports for pandas and numpy - atleast ```import pandas as pd from numpy import NaN``` likely you didnt run imports in your second run and that was why it crashed on second line.I have run the requested imports. For some reason, df3['diff']=df3['initial cost']-df3['Total Est. Fee'] is no longer working, so I tested the code on a different column. It still is not working for some reason. Sorry if I am annoying you all :( df3['initial cost'][df3['initial cost'] < 0] = NaN Reply zivoni Minister of Silly Walks Posts: 331 Threads: 2 Joined: Feb 2017 Reputation: 65 Apr-24-2017, 05:35 PM As both snippsat and nilamo said before, you make it harder by not posting informations about your code and/or errors. Its hard to help when you just say "is not working", neither its helping when you are working with pandas without knowing that you are working with pandas ... Your code with changes suggested in my previous post works fine for me: ``````Output:In : import pandas as pd    ...: from numpy import NaN    ...: dict3={'initial cost': ['\$75000.00','\$0.00','\$30000.00','\$1500.00','\$19500.00'],'Total Est. Fee':['\$986.00','\$1144.00','\$522.50', '\$    ...: 225.00', '\$389.50']}    ...: df3=pd.DataFrame(dict3)    ...: df3['Total Est. Fee']=df3['Total Est. Fee'].str.split('\$')    ...: df3['Total Est. Fee']=df3['Total Est. Fee'].str.get(1)    ...: df3['initial cost']=df3['initial cost'].str.split('\$')    ...: df3['initial cost']=df3['initial cost'].str.get(1)    ...: df3['Total Est. Fee']=pd.to_numeric(df3['Total Est. Fee'],errors='coerce')    ...: df3['initial cost'] = pd.to_numeric(df3['initial cost'], errors='coerce')    ...: df3['diff']=df3['initial cost']-df3['Total Est. Fee']    ...: df3['diff'][df3['diff'] < 0] = NaN    ...: In : df3 Out:    Total Est. Fee  initial cost     diff 0           986.0       75000.0  74014.0 1          1144.0           0.0      NaN 2           522.5       30000.0  29477.5 3           225.0        1500.0   1275.0 4           389.5       19500.0  19110.5`````` Reply smw10c Silly Frenchman Posts: 35 Threads: 13 Joined: Mar 2017 Reputation: 0 Apr-24-2017, 07:16 PM (This post was last modified: Apr-24-2017, 07:17 PM by smw10c.) For the first bit of code where I subtract initial cost from Total estimated fee this is the error I get: TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('

Forum Jump:

### User Panel Messages ##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020