Matplotlib not imported, script opens plot anyway - python

So I have this simple python code
import pandas as pd
import quandl
df = quandl.get("WIKI/GOOGL", api_key="censored")
print(df.head())
And for some reason when I execute it, it prints out
1.7320508075688772
2.23606797749979
2.6457513110645907
3.0
And it also opens up this pyplot https://i.imgur.com/lHi3X2y.png
And when closing the image it prints out the actual df.head, but also
[5 rows x 12 columns]
There was no way for that to happen, so I got super confused.

Related

How can I import a dataframe from another script

I have a little beginner issue doing imports between scripts, maybe someone can help?
I wrote 2 Modules:
- B01_Import_CSV_V1.py
In the import script, i did load a .csv and saved it as a pandas dataframe (named "df").
import pandas as pd
global df
global df2
#%% load file via static path
if True:
tester=r'C:\local_calc\Python_DemoFiles\20220219_70788_406_02_C10_275C_Ges.csv'
df=pd.read_csv(tester, sep=';',decimal=",", skipfooter=1300, engine='python')
print ('FINISHED loading file ' + tester)
- plot.py
The plot script should process this data.
#%% Imports
import matplotlib.pyplot as plt
import B01_Import_CSV_V1 #<- this works
#from B01_Import_CSV_V1 import df #<- this does not work
#%%% plot
fig=plt.figure(figsize=(15,8))
ax=fig.add_subplot(111)
ax2=ax.twinx()
ax3=ax.twinx()
ax.plot( df['70788.1.E602000_W1:6'], c = 'b')
My questions:
It will only work, if i use the code "import B01_Import_CSV_V1" in the first script.
If I use "from B01_Import_CSV_V1 import df", it won't.
-> Why is that?
If I integrate the code of Import-Script in a main function (if name=="main": ), i learned that this code won´t be executed anymore while import.
The plot script won´t work either then.
-> Does this mean, I have to execute the csv-Import within the plot script?
Is there any way to execute scripts separately and then use the variables from each other?
Just make a function and import that in the other file:
import pandas as pd
def load_df():
tester=r'C:\local_calc\Python_DemoFiles\20220219_70788_406_02_C10_275C_Ges.csv'
df=pd.read_csv(tester, sep=';',decimal=",", skipfooter=1300, engine='python')
print ('FINISHED loading file ' + tester)
return df
Then use in the other:
from B01_Import_CSV_V1 import load_df
df = load_df()

Cannot print the first 5 rows using df.head(5)

Complete noob here. I've been trying the following instructions to access a data set on Kaggle and to read the first 5 rows.
https://towardsdatascience.com/simple-and-multiple-linear-regression-with-python-c9ab422ec29c
I'm using spyder and when I run the following code, I only obtain a runfile wdir= comment in the console
Following is the Code:
import pandas as pd
df=pd.read_csv('weight-height.csv')
df.head(5)
Output:
Code and Console Output
The medium post is probably using jupyter notebooks which will take the last line and put it as formatted output in a cell below it without a print. In a regular python script / idle or other IDEs, you need to actually use the print function to print to the terminal/console.
import pandas as pd
df = pd.read_csv('weight-height.csv')
print(df.head(5))

No lines of output shown after importing pandas

I installed pandas through pip, but when I import it, the code runs but no output is shown at all, right after the import statement.
Here's a sample of my code
import xlrd, xlwt
print("1")
import pandas as pd
print("2")
from math import trunc
1 is printed, but 2 isn't. After 1 is printed, the script just hangs for a few seconds and terminates. This occurs regardless of the code written below the import statement. I also seem to get the same error for the openpyxl module. Does anyone know a fix to this?

python file does not run properly on Spyder

I am using Spyder as my python IDE.
I tried run this python code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
path = os.getcwd() + '\ex1data1.txt'
data = pd.read_csv(path, header = None, names = ['Population', 'Profit'])
data.head()
In theory it is supposed to show me a table of data since I am using
data.head()
at the end
but when I run it, it only shows :
I thought it was supposed to display a new window with the data table.
What is wrong?
You are calling data.head() in .py script. The script is supposed to only read the data, not print it. Try print(data.head())
You want to print your data.head() so do that...
print(data.head())

plot in Pandas immediately closes

I have a problem of plotting the data. I run the following python code:
import pandas as pd
df = pd.read_csv("table.csv")
values = df["blah"]
values.plot()
print 1
df['blahblah'].plot()
print 2
My output is:
50728417:Desktop evgeniy$ python test2.py
1
2
50728417:Desktop evgeniy$
And finally I see how launches python icon( picture of rocket, by the way , what is it?) in my Dock( using mac os), and then it disappears. Printed numbers 1,2 show me that no error exist. So have no idea what to do next.
The problem is that pandas's DataFrame.plot does not block. Therefore the figures are closed when your program ends. Pandas uses matplotlib internally so you can circumvent this by calling matplotlib.pyplot.show with block=True like so:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("table.csv")
values = df["blah"]
values.plot()
print 1
df['blahblah'].plot()
print 2
plt.show(block=True)
This way the program will only end when plt.show returns, which is after you closed all figures.

Categories