How do I import data and packages from REST APIs - python

I'm currently trying to learn Python through coursera and I'm trying to run the code but I'm running into an error when I try to install nba_api. Below I have pasted my code
!pip install nba_api
# I get an 'invalid syntax error' here ^^
import pandas as pd
import matplotlib.pyplot as plt
x={'a':[11,21,31],'b':[12,22,32]}
print(x)
df=pd.DataFrame(x)
print(type(df))
print(df.head())
from nba_api.stats.static import teams
import matplotlib.pyplot as plt
Any ideas what I'm doing wrong/missing here?

! is a shorthand for calling out to the shell that is only in the REPL. You can't use this in Python files.
Instead you should put your code in a package and have a dependency on nba_api, when someone installs your package, nba_api package would also be installed.

Related

What can I do when getting "pd is not defined"? it seems to be combined with a dying kernel

Does anyone know the fix for this "pd is not defined"? it seems to be combined with a dying kernel. I'm using Jupyter Notebook.
I had started with these imports and didn't get an error message so I was assuming that pandas was imported successfully
import pandas as pd
import numpy as np
Tried updating my Python version to 3.11
This should not be a python problem. Since import pandas as pd did not fail, you should try print out print(pd) and see where pandas is installed and maybe verify it is is properly installed. It should print out something like lib/python3.x/site-packages/pandas/__init__.py'.

"No module named 'lab_utils_common'"

I was trying to run this program below but it's showing the error "No module named 'lab_utils_common'", what can I do to solve this problem?
import numpy as np
import matplotlib.pyplot as plt
from lab_utils_common import plot_data, sigmoid, dcl
plt.style.use('./deeplearning.mplstyle')
lab_utils_common package is either not publicly available or not actively maintained.
Two possible scenarios:
lab_utils_common is a published package, but not installed. Use pip or conda to install.
lab_utils_common is supposed to be a local module written to lab_utils_common.py, but is missing. See more on modules if interested.

How can address this python plotly.express error: "Mime type rendering requires nbformat>=4.2.0 but it is not installed"

I faced a problem in python and was wondering if someone is able to help me solve it.
I get below error when I try to use the the "show()" function for “plotly.express” library.
ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not
installed
I tried below 2 methods to solve, it still does not work. They are the recommended solutions by many sources online.
1 - pip install ipykernel
2 - pip install --upgrade nbformat
Any idea how to fix it?
Thank you,
My code:
import pandas as pd
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
data = pd.read_csv('daimond_data.csv')
data = data.drop('Unnamed: 0', axis=1)
fig = px.scatter(data_frame=data, x='carat', y='price', size='depth',color='cut', trendline='ols' )
print(fig.show())

The plotly.grid_objs module is deprecated for jupyter notebook offline plot

I am following a tutorials in plotly official website. I am using jupyter notebook. and I got an import error for plot.grid_objs. I am using plotly 4.0. Any suggestion to fix it? Thanks
https://plotly.com/python/v3/gapminder-example/
ImportError:
The plotly.grid_objs module is deprecated,
please install the chart-studio package and use the
chart_studio.grid_objs module instead
import plotly as py
import plotly.figure_factory as FF
from plotly.grid_objs import Grid, Column
import pandas as pd
import time
import pickle
filename_pickle='dataset.pkl'
try:
dataset=pd.read_pickle(filename_pickle)
except FileNotFoundError:
url = 'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
dataset = pd.read_csv(url)
dataset.to_pickle(filename_pickle)
table = FF.create_table(dataset.head(10))
py.offline.iplot(table) # table is inline inside jupyter notebook
ImportError:
The plotly.grid_objs module is deprecated,
please install the chart-studio package and use the
chart_studio.grid_objs module instead
Looks like the tutorial is for a much older version of plotly. Try:
pip install plotly==2.0.1
in your terminal and then rerun the Python code.
By referencing the documentation, you can find that do something as seen here:
import chart_studio
from chart_studio.grid_objs import Column, Grid
It also says to Replace plotly.grid_objs with chart_studio.grid_objs in the version 4 migration guide. In this case, a version downgrade is not necessary.

no module named sys and time

I wrote a GUI, for which I used these imports:
import os
import sys
import serial
import scipy
import string
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from collections import deque
from numpy import array
from pylab import xlabel, ylabel, subplot
from scipy.fftpack import fft
from pylab import *
There is a red line below sys and time, I am using pycharm community edition 4.5.3, it is showing the reason for this error is 'no module named sys' and same for time.
But when i tried to run it, it works perfectly.
What is the reason behind it and will it affect my code in future?
Change the python interpreter from python to python2.7. It's helped me.
This is something I just ran into with Intellij 2017.3.4 where it could find every module except sys and time in the editor, but everything would run fine. I had both 2.7 and 3.5 version of python and it did not seem to matter which one I selected as the SDK. I tried adding and removing them.
When I went to Project Structure -> Platform Settings -> SDKs -> Python 3.5 -> Packages it prompted a warning that Python packaging tools not found. and had an install link. I installed it and the editor no longer complained about sys and time. When I switched SDKs to 2.7 (without installing the packaging tools) it complained again.
So I am not exactly sure what is happening that that seemed to fix it for me it other people run into this problem.

Categories