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())
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've so many different ways and it is still saying file not found
I'm running the code in a Jupyter Notebook.
I'd rather run the file from wherever it is. Here is the infomation for its location
Have I generated the correct code (below is the code).
import numpy
numpy.loadtxt(fname='C:\Desktop\swc-python\data\inflammation-01.csv', delimiter=',')
Also tried this but it did not work:
import numpy
numpy.fname = ('C:\Desktop\swc-python\data\small-01.csv')
openfname = open(fname,'r')
Also, an you save a Jupyter notebook in the same directory as the infomation.
these are some examples with pandas, and os, maybe they could help
they use slash, not backward slash (this option, or the option below)
# import pandas as pd
pd.read_csv("C:/Users/<Insert your user>/Desktop/code/Exercise Files/us_baby_names.csv")
or
(option below), change the current directory,
# import os and pandas library
import os
import pandas as pd
# show current working directory, change it, show it again
os.getcwd()
os.chdir('C:/Users/<Insert your user>/Desktop/code/Exercise Files/')
os.getcwd()
pd.read_csv("us_baby_names.csv")
I want to import excel data in jupyter notebook,in the python 3.7, but I got the following errors, can anyone explain to me the solution of the problem awaiting for your kind response.
Make sure your path is correct for the excel file. Also, pay attention to slash '/' It is NOT '\'.
import pandas as pd
import os
my_path = 'C:/Users/user/Desktop/2nd Semester Research Topic'
df = pd.read_excel(os.path.join(my_path,'com.xlsx'))
I have a CSV file that I am uploading into Jupyter and I am trying to delete multiple columns at once. I thought the "DEL" command would be the best but I can't get it to work.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
% matplotlib inline
tmbd_movies = pd.read_csv('tmdb-movies.csv')
tmbd_movies.head()
del(tmbd_movies['imdb_id','homepage','tagline','keywords','overview'])
The goal was to remove the following columns:
imdb_id','homepage','tagline','keywords','overview
You want this:
tmbd_movies.drop(['imdb_id','homepage','tagline','keywords','overview'], 'columns', inplace=True)
I realized that there may be something wrong in my local dev env just now.
I tried my code on colab.
it worked well.
import pandas as pd
df = pd.read_excel('hurun-2018-top50.xlsx')
thank u all.
please close this session.
------- following is original description ---------
I am trying to import excel with python and pandas.
I already pip installed "xlrd" module.
I googled a lot and tried several different methods, none of them worked.
Here is my code.
import pandas as pd
from pandas import ExcelFile
from pandas import ExcelWriter
df = pd.read_excel('hurun-2018-top50.xlsx', index_col=0)
df = pd.read_excel('hurun-2018-top50.xlsx', sheetname='Sheet1')
df = pd.read_excel('hurun-2018-top50.xlsx')
Any response will be appreciated.