Guido Muscioni
Guido Muscioni

Reputation: 1295

Global DataFrame not updated python

I have this code:

latitude = pd.DataFrame()
longitude = pd.DataFrame()

datasets = []

datasets.append(latitude)
datasets.append(longitude)

def fun(key, job):
    global latitude
    global longitude

    if(key == 'LAT'):
        latitude = job.copy()
    elif(key == 'LON'):
        longitude = job.copy()

for d in datasets:
    print(d.shape)

Following this: Why does assigning to my global variables not work in Python? I have used the global keyword to ensure that the variables are correctly referenced. However, the values are not updated. Any advice? I have already verified that the if statements are correct as I am able to print something in them.

OUTPUT:

(0, 0)
(0, 0)

Moreover, I am using spyder and I can see that the list contains empty dataframe and also the global variable are empty.

Upvotes: 1

Views: 1457

Answers (1)

ddor254
ddor254

Reputation: 1628

o.k , the problem you have is that you make your program to point to some value in the datasets.append(latitude) and when you change the global latitude latitude = job.copy() , you changing the value of the global variable and not the one in your list datasets.

if you want to see the changed value print out latitude and you will see that the value has changed. or change the value in datasets[0] for latitude value in your loop.

Upvotes: 2

Related Questions