问题
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:
(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.
来源:https://stackoverflow.com/questions/59271717/override-global-variable-inside-function-not-working-with-spyder-4