VS Code Output result different from Terminal result - python

I just started learning python about a week ago and I am using VS code.
I am trying to run the following code:
import pandas as pd
df = pd.read_csv('ClassMarks.csv')
pd.set_option('display.max_rows', 85)
df['Final Marks'] = df['CPQ']+df['HW']+df['Tutorials']+df['Tests']
print(df)
in the Output terminal it gives:
File "/Users/User1/Desktop/test", line 1, in <module>
import pandas as pd
ImportError: No module named pandas
while the Terminal gives me the result table I expect, with no errors.
Does anyone knows what is happening or how to fix it?
Thanks in advance!
Edit:
The problem I had was, that my Output window was using python 2.7.16 as default. To change it to python 3.8.2, I found the answer in this video:
https://www.youtube.com/watch?v=06I63_p-2A4,
starting from min 43:00.
I hope this helps others!

The problem I had was, that my Output window was using python 2.7.16 as default. To change it to python 3.8.2, I found the answer in this video: https://www.youtube.com/watch?v=06I63_p-2A4, starting from min 43:00.
I hope this helps others!

Related

python not showing values in max()

The following code ran in PYCHARM editor
import pandas as pd
df=pd.read_csv('Train_UWu5bXk.csv')
df.min()
the output is not showing any value
C:\Users\krishna\PycharmProjects\pdproject\Scripts\python.exe
C:/Users/krishna/Python37-32/Scripts/phythonncode/load1pandas.py
Process finished with exit code 0
someone can help me.
the problem is solved with the following code in python
import pandas as pd
df=pd.read_csv('Train_UWu5bXk.csv')
print(df.min())

import excel data in Jupyter notebook faced with problem

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'))

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))

scikit-surprise: python cannot find module even though pip lists it as installed

I am trying to use the scikit-surprise module to build a recommender system however I am having an error in getting it to compile.
I am receiving the ImportError: Cannot import name "Reader" error
My class is as follows
import pandas as pd
from surprise import Reader, Dataset
userReviewsFilePath ="UserReviewsFirst5000WithHeadings.csv"
ratings = pd.read_csv(userReviewsFilePath) # reading data in pandas df
ratings_dict = {'recipeID': list(ratings.recipeID),
'rating': list(ratings.rating),
'userID': list(ratings.userID)}
df = pd.DataFrame(ratings_dict)
reader = Reader(rating_scale=(1, 5))
data = Dataset.load_from_df(df[['recipeID', 'rating', 'userID']], reader)
pip show says that version 1.0.6 is installed
I think your problem come from the installation... I installed "surprise" and past your code and it worked:
import pandas as pd
from surprise import Reader, Dataset
print(Reader) # or just print(surprise) if you import surprise
out:
<class 'surprise.reader.Reader'>
Start by re-install surprise and tell us.
If you have more than one version of python, do:
which pip
to see if you installed surprise on the used version of python
I think it's in surprise.reader: https://surprise.readthedocs.io/en/stable/reader.html
Your code should read:
from surprise.reader import Reader
from surprise.dataset import Dataset
Edit: I checked the instructions again which seem to contradict this, and give your original code as the correct example. https://surprise.readthedocs.io/en/stable/getting_started.html#getting-started
So maybe they add their own shortcuts? Either way, it seems like this isn't the correct solution, sorry. (Unless it works, in which case their instructions might be out of date.)
Edit 2: They do alias it, so "from surprise import Reader" should indeed have worked: https://github.com/NicolasHug/Surprise/blob/master/surprise/init.py#L19
I think you need to do
from surprise.reader import Reader

Vincent not creating any json

I have been trying to create a Vincent time series line plot. My code is as follows:
#!/usr/bin/env python
import pandas as pd
import numpy as np
import vincent
#test data
df2 = pd.DataFrame({ 'A' : 1., 'B' : pd.Timestamp('20130102'),'C' : pd.Series(1,index=list(range(4)),dtype='float32'),'D' : np.array([3] * 4,dtype='int32'), 'E' : pd.Categorical(["test","train","test","train"]), 'F' : 'foo' })
vis = vincent.Line(df2) # test
vis.axis_titles(x='Time', y='Freq')
vis.legend(title='Words')
vis.to_json('chart.json')
vis.display()
I get no output (no display or chart.json created) or any errors. The other similar problems on here are due to Ipython notebook or Canopy issues, for example this; I am not using Ipython, notebook or Canopy. My question is: why is there no json created?
EDIT: OK maybe I am using Ipython without knowing it! I get this output:
<IPython.core.display.HTML at 0x7f980791e2d0>
However adding vis.core.initialize_notebook()from this solution does not help.
It was an issue with Eclipse. My working directory was for some reason set to a subdirectory where the json file WAS being created. I post this in case someone else is as stupid as me.
EDIT: I added the Eclipse tag. Should have been there in the beginning.

Categories