How to import Pandas package?

PandasServer Side ProgrammingProgramming

Pandas is a python package that has a set of tools (nothing but functions) that can deal with data. By using this set of tools we can perform required tasks on our data.

To get all these tools into our python workspace we need to import the package first. To do this importing process we have to use the python import keyword.

By default, Python doesn’t load all of the libraries available to it. Due to this, we need to add an import statement to our code to utilize the library tools (functions).

The syntax of importing a library is the import keyword followed by the library name. Let’s see in the below block of code for an example.

Example

import pandas

Output

Using the above line of code we can get all features available in the pandas package, and that can be used further. The result of this line of code won’t return any output. We will get an error message like ‘ModuleNotFoundError’ if you give the wrong name of our package.

To use a function or a tool that is available in our package we need to use the package name and function name for each time. Let’s see the syntax below.

Example

pandas.get_dummies()

Explanation

By adding the library name with a . before the function name tells Python where to find the function. Here get_dummies is a pandas function to convert categorical data into dummy variables.

If you want to change the library name to a shortened name like a nickname. We can add as

If we want to give the library a nickname to shorten the command, which can be called an alias. This can be done by using an as keyword in python. Let’s see an example by adding a common nickname to our pandas library below.

Example

Import pandas as pd
pd.read_csv()

Explanation

In the example above, we have imported Pandas as pd. This means we don’t have to type out pandas each time to call any Pandas function. Here pd is pandas nickname and read_csv() is the pandas method that will read a file with the .csv extension.

raja
Published on 18-Nov-2021 06:18:42

Advertisements