In my final put up, I confirmed you how you can use pip to put in 4 well-liked packages for Python. In the present day I wish to present you the fundamentals of how you can import and use Python packages. We are going to study some necessary Python ideas and jargon alongside the way in which. I shall be utilizing the pandas package deal within the examples beneath, however the concepts and syntax are the identical for different Python packages.
Utilizing packages with import modules
pandas is a well-liked Python package deal used for importing, exporting, and manipulating information. The package deal comprises totally different modules for working with totally different information constructions reminiscent of sequence, information frames, and panels.
Let’s start by typing python which pandas to confirm that pandas is put in in our system.
. python which pandasPython38libsite-packagespandas__init__.py'>
If you don’t see a path in your outcomes, you have to to put in the pandas package deal as described in my earlier put up.
Subsequent, we are able to inform Python that we want to use the pandas package deal by typing import pandas on the high of our code block. This imports the pandas module from the package deal.
python import pandas finish
We are able to then learn Stata’s auto dataset from the Stata Press web site right into a pandas information body named “auto” utilizing the pandas.read_stata() technique.
python
import pandas
auto = pandas.read_stata("http://www.stata-press.com/information/r16/auto.dta")
finish
The time period technique is used to explain a perform inside a module. The tactic read_stata() is a perform inside the pandas package deal that reads Stata datasets and converts them to pandas information frames.
Import modules utilizing an alias
The pandas module contains many strategies, and we might finally develop uninterested in typing pandas earlier than every technique. We are able to keep away from typing the module title by giving it an alias. We are able to assign an alias to a module by typing import modulename as alias. Within the code block beneath, I’ve typed import pandas as pd to assign the alias pd to pandas. Now, I can use the read_stata() technique by typing pd.read_stata() moderately than typing pandas.read_stata().
python
import pandas as pd
auto = pd.read_stata("http://www.stata-press.com/information/r16/auto.dta")
finish
Utilizing strategies and lessons inside modules
Modules are a method of subdividing the performance of a package deal for various conditions. A module can have a set of strategies, lessons, and variables outlined. We are able to consult with them inside a module in our Python statements. For instance, the DataFrame class is contained within the pandas.core.body module inside the pandas package deal. Typically, we merely consult with a category inside a package deal and omit the title of the module. For instance, the fourth line within the code block beneath makes use of the imply() technique within the DataFrame class of the pandas package deal to estimate the imply of mpg and weight.
python
import pandas as pd
auto = pd.read_stata("http://www.stata-press.com/information/r16/auto.dta")
pd.DataFrame.imply(auto[['mpg','weight']])
finish
The code block above produces the next output.
. python
----------------------------------------------- python (sort finish to exit) ------
>>> import pandas as pd
>>> auto = pd.read_stata("http://www.stata-press.com/information/r16/auto.dta")
>>> pd.DataFrame.imply(auto[['mpg','weight']])
mpg 21.297297
weight 3019.459459
dtype: float64
>>> finish
--------------------------------------------------------------------------------
Importing strategies and lessons from modules
You may also import lessons from modules inside packages and omit the module title or alias whenever you use the category. The third line of the code block beneath imports the DataFrame class from the pandas package deal. Word that capitalization is necessary. Now, you need to use the imply() technique by typing DataFrame.imply() moderately than pd.DataFrame.imply().
python
import pandas as pd
from pandas import DataFrame
auto = pd.read_stata("http://www.stata-press.com/information/r16/auto.dta")
DataFrame.imply(auto[['mpg','weight']])
finish
Importing features and lessons utilizing an alias
It’s possible you’ll develop uninterested in typing DataFrame.imply() each time you estimate a imply. Fortuitously, you possibly can assign an alias to a category by typing from modulename import classname as alias. Within the third line of the code block beneath, I’ve assigned the alias df to the DataFrame class by typing from pandas import DataFrame as df. Now, I can use the imply() technique by typing df.imply() moderately than DataFrame.imply().
python
import pandas as pd
from pandas import DataFrame as df
auto = pd.read_stata("http://www.stata-press.com/information/r16/auto.dta")
df.imply(auto[['mpg','weight']])
finish
Evaluate and conclusion
Let’s overview the ideas and jargon we have now realized utilizing the next diagram.
A Python package deal is a group of modules. Every module can comprise a set of strategies, lessons, and variables. For instance, the pandas package deal features a assortment of lessons and strategies used for importing, exporting, and managing information.
A technique is a perform that may settle for arguments and does one thing. Strategies will be a part of a module or a category.
The strategies inside packages are sometimes subdivided into modules and lessons. Packages and lessons can every embrace many strategies. We should import modules or lessons earlier than we are able to use their strategies.
You’ll be able to learn extra in regards to the pandas package deal within the pandas person information.
Now, we have now laid all of the groundwork we want and are prepared for the enjoyable half! In my subsequent put up, I’ll present you how you can use Stata to estimate marginal predictions from a logistic regression mannequin and use Python to create a three-dimensional floor plot of these predictions.
