I

Importing Libraries

Libraries provide additional functionality in an organized and packaged way. We mainly work with the Pandas library throughout this book, but there are times when we will import other libraries. You will see many different ways to import a library. The most basic way is to simply import the library by its name.

import pandas

When we import a library, we can use its functions within Pandas using dot notation.

print(pandas.read_csv('data/concat_1.csv'))
   A  B  C  D
0 a0 b0 c0 d0
1 a1 b1 c1 d1
2 a2 b2 c2 d2
3 a3 b3 c3 d3

Python gives us a way to alias libraries. This allows us to use an abbreviation for longer library names. To do so, we specify the alias after the as statement.

import pandas as pd

Now, instead of referring to the library as pandas, we can use our abbreviation, pd.

print(pd.read_csv('data/concat_1.csv'))
   A  B  C  D
0 a0 b0 c0 d0
1 a1 b1 c1 d1
2 a2 b2 c2 d2
3 a3 b3 c3 d3

Sometimes, if only a few functions are needed from a library, we can import them directly.

from pandas import read_csv

This will allow us to use the read_csv() function directly, without specifying the library it is coming from.

print(read_csv('data/concat_1.csv'))
   A  B  C  D
0 a0 b0 c0 d0
1 a1 b1 c1 d1
2 a2 b2 c2 d2
3 a3 b3 c3 d3

Finally, there is a method that enables users to import all the functions of a library directly into the namespace.

from pandas import *
from numpy import *
from scipy import *

This method is not recommended because libraries contain many functions, and a function can mask an existing function. For example, if we import all the functions from numpy and from scipy, which mean() function is used? It’s not as clear as saying numpy.mean() and scipy.mean().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.39.59