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.
Related
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()
I have a dataframe of emails that has three columns: From, Message and Received (which is a date format).
I've written the below script to show how many messages there are per month in a bar plot.
But the plot doesn't show and I can't work out why, it's no doubt very simple. Any help understanding why is much appreciated!
Thanks!
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('XXX')
df = df[df['Message'].notna()]
df['Received'] = pd.to_datetime(df['Received'], format='%d/%m/%Y')
df['Received'].groupby(df['Received'].dt.month).count().plot
A pyplot object (commonly plt) is not shown until you call plt.show(). It is designed that way so you can create your plot and then modify it as needed before showing or saving.
Also checkout plt.savefig().
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.
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())
This question already has answers here:
How to show matplotlib plots?
(6 answers)
Closed 5 years ago.
I have a pandas data frame wit the following properties:
Name: df_name,
Concerned Column: col1
If I want to plot a column, I can execute the following code in python shell(>>>) or ipython notebook.
>>>df_name['col1'].plot(kind='bar')
However, I want to use the same function in a script and execute from command line, the plot doesn't appear.
The script I want to write looks like the following:
import pandas as pd
.
.
.
df_name=pd.read_csv('filename')
# Printing bar chart of 'col1'
df_name['col1'].plot(kind='bar')
Any Ideas how to make it execute from a script?
I think, you need to import matplotlib.pyplot and to use show method like in example.
import pandas as pd
import matplotlib.pyplot as plt
df_name=pd.DataFrame([1,2,3])
df_name[0].plot(kind='bar')
plt.show()