1
votes

I try to override globally defined Dataframes from within a function. Somehow the global values do not change, printing the dataframes within the functions works with the expected values.

import pandas as pd

rawData = pd.read_csv("music.csv")
appTitles =pd.DataFrame #also with pd.DataFrame() wont get affected

def loadTitles(df=rawData):
    global appTitles
    appTitles = pd.DataFrame({'Title' : df['title']})
    print(appTitles) 
return appTitles #(second 'print' to screen)

#RUN FILE
#CALL FUNCTION: loadTitles() #-> No Variable appTitles

The appTitles variable in the variable explorer remains empty or nonexistent (depending use of pd.DataFrame/pd.DataFrame()) Thank you!

EDIT: After @4turkuaz commented that on his environment everthing works as expected, I tried Python console within Windows cmd and also on Spyder3 with Python 3.7.4 & IPython 7.8.0 where also everything worked fine.

Only on Spyder4 with Python 3.7.5 & IPython 7.10.1 it won't work. Maybe a bug? I don't know...

EDIT: Downgrading Spyder4 to Python 3.7.4 & IPython 7.8.0 did not solve the problem. I'm assuming that something within Spyder4 or the Anaconca Environment even though I installed it over and over is not correct.

1
Well actually, it worked as you expected on my environment with Python 3.6. - Berkay
I'm on 3.7 using Spyder as IDE - muka90
Can you downgrade it to 3.6 and try again? - Berkay
Just tried within python console on windows and it works as expected. Weird. - muka90

1 Answers

4
votes

(Spyder maintainer here) You need to go to the menu

Run > Configuration per file

and activate the option called Run in console's namespace instead of an empty one, for you to be able to modify global variables in your code.