0

In the code below I wanted to make a = '5' be the same like saying df['Child'].iat[y] = 5. In other terms, I want a to refer to the object itself rather than becoming its value.

In visual basic you can say something like set a = df['Child'].iat[y] so I was looking for the python equivalent of this. The idea is to make the code simple and clean.

I am not interested in making 'Parent' column = 5. That's just as an example. If your solution is correct then saying a = '5 should be identical to saying df['Child'].iat[y] = 5 and should change the column value.

Thanks in advance!

import pandas as pd
df = pd.DataFrame(['MICROSOFT','CISCO', 'CISCO System', 'CISCO Systems', 'CISCO Systems CANADA', 'CISCO Systems CANADA Corporation', 'CISCO Systems CANADA Corporation Limited', 'IBM', 'Apple','Apple Corp'], columns=['Child'])
df['Parent'] = ''
display(df)

for y in range (1,df.shape[0]):
    a = df['Child'].iat[y]
    a = '5'

display(df)

enter image description here

Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29
  • 1
    So after `a = df['Child'].iat[y]`, you want `a = 5` to also assign `5` to `df['Child'].iat[y]` ? You expect the Parent column to be 5? – tdelaney May 02 '20 at 05:51
  • Yes that's correct. I simply want to say a = 5 to be identical in its effect to saying df['Child'].iat[y] = 5. it makes the code much cleaner to simply write a = 5 – Chadee Fouad May 02 '20 at 05:57
  • 2
    A variable already binds to “the object itself”. However, all _assignments_ to a variable are independent of the object it represents/evaluates to. tldr: not possible with variables directly. – user2864740 May 02 '20 at 05:57
  • 2
    You can have a look at https://nedbatchelder.com/text/names.html and https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference for detailed explanations of how things work. – Thierry Lathuille May 02 '20 at 05:59
  • @user2864740 So basically are you saying that this is not possible in Python? In Excel VBA for example you can say something like set mycell = thisworkbook.sheets('sheet1').range('A1')..now if you write mycell = 5 then cell A1 will become 5. That's what I am after...that's not possible in Python? – Chadee Fouad May 02 '20 at 06:00
  • 2
    This fundamentally not how the semantics of Python assignment works. Note, the distinction you make doesn't make sense, it *is* the object itself. – juanpa.arrivillaga May 02 '20 at 06:01
  • 2
    You can't do it in python. Variables hold references to objects but not the actual physical address of the object. `a = 5` simply rebinds the variable `a` to 5. `a` has no reference to the place its object originally came from. Its even worse in the numpy/pandas case where `df['Child'].iat[y]` creates a python object from the machine level integer in the array. – tdelaney May 02 '20 at 06:02
  • @tdelaney got it. So unfortunately the code has to look lengthy. Thanks for the explanation :-) – Chadee Fouad May 02 '20 at 06:05
  • In the VB case `mycell = thisworkbook.sheets('sheet1').range('A1')` mycell isn't an int, its a reference into the spreadsheet. I don't know how youd do the same thing in pandas. – tdelaney May 02 '20 at 06:06
  • Maybe pandas slicing can help, not sure. – tdelaney May 02 '20 at 06:07
  • 2
    You could do `a = df['Child'].iat` and then `a[y] = 5` – tdelaney May 02 '20 at 06:08
  • didn't work but a close solution worked: ```a = df['Parent'] a.iat[y] = '5'``` Many thanks for your help!! :-) – Chadee Fouad May 02 '20 at 06:19

1 Answers1

0

After some research, below is the closest solution that worked. Not perfect but better than the original case.

import pandas as pd
df = pd.DataFrame(['MICROSOFT','CISCO', 'CISCO System', 'CISCO Systems', 'CISCO Systems CANADA', 'CISCO Systems CANADA Corporation', 'CISCO Systems CANADA Corporation Limited', 'IBM', 'Apple','Apple Corp'], columns=['Child'])
df['Parent'] = ''
display(df)

for y in range (0,df.shape[0]):
    a = df['Parent']
    a.iat[y] = '5'

display(df)

enter image description here

Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29