Importing Libraries with Aliases in Python
In Python, it's common practice to import libraries using aliases β short names that make code easier to write and read. This is especially common in data science and analytics.
Example
import pandas as pd
import seaborn as sns
pandasis imported aspdseabornis imported assns
These aliases are not mandatory but are standard practice in the Python community. It gives the following advantages:
- Shorter code: Instead of
pandas.DataFrame()orseaborn.load_dataset(), you can writepd.DataFrame()orsns.load_dataset(). - Better readability:
pd,sns,np, andpltare instantly recognisable to anyone familiar with Python for data analysis. - Less typing: Aliases reduce repetition when using library functions many times.
Common Aliases
| Library | Alias |
|---|---|
| pandas | pd |
| numpy | np |
| matplotlib.pyplot | plt |
| seaborn | sns |
Without an Alias
You can still import the full library:
import seaborn
df = seaborn.load_dataset("tips")
But this is verbose and non-idiomatic for most data analysis work.