🐼 Unit 1 – Pandas Series · Lesson 06
Introduction to Pandas
Python libraries for data
A library is a collection of ready-made modules. For data work three libraries are used together:
🔢
NumPyFast arrays and maths on numbers (ndarray).🐼
PandasTables of data: Series and DataFrame; read CSV, clean, group, analyse.📈
MatplotlibDraw line, bar, pie charts and histograms.Installing pandas
Command prompt
pip install pandas matplotlib
NumPy is installed automatically with pandas. Then import the libraries in your program:
Python
import pandas as pd
import numpy as np
print('pandas version:', pd.__version__.split('.')[0] + '.x')
print('numpy version:', np.__version__.split('.')[0] + '.x')pandas version: 2.x
numpy version: 2.x
Data structures in pandas
Series
- One-dimensional
- One column of values
- Has an index (labels)
- Same data type (usually)
DataFrame
- Two-dimensional
- Rows and columns (a table)
- Row index + column names
- Each column can have its own type
First look: Series
Python
import pandas as pd marks = pd.Series([78, 92, 65], index=['Asha', 'Ravi', 'Neha']) print(marks)
Asha 78
Ravi 92
Neha 65
dtype: int64
| values | |
|---|---|
| Asha | 78 |
| Ravi | 92 |
| Neha | 65 |
First look: DataFrame
Python
import pandas as pd
data = {'Name': ['Asha', 'Ravi', 'Neha'],
'Maths': [78, 92, 65],
'IP': [88, 81, 95]}
df = pd.DataFrame(data)
print(df) Name Maths IP
0 Asha 78 88
1 Ravi 92 81
2 Neha 65 95
Series vs NumPy array
Python
import numpy as np import pandas as pd arr = np.array([10, 20, 30]) s = pd.Series([10, 20, 30], index=['a', 'b', 'c']) print(arr[1], s['b'], s.iloc[1]) # array: position only; Series: label or position print(arr + 5) print(s + 5)
20 20 20
[15 25 35]
a 15
b 25
c 35
dtype: int64
Python
import pandas as pd a = pd.Series([1, 2, 3], index=['x', 'y', 'z']) b = pd.Series([10, 20, 30], index=['z', 'x', 'w']) print(a + b) # values are matched by LABEL, missing labels give NaN
w NaN
x 21.0
y NaN
z 13.0
dtype: float64
| NumPy ndarray | Pandas Series |
|---|---|
| Accessed by integer position only | Accessed by label or position |
| Operations match elements by position | Operations match elements by index label |
| Missing values are not natural | Missing values shown as NaN |
| Faster for pure maths | Easier for labelled data |
Why pandas?
- Reads and writes CSV, Excel and SQL data in one line.
- Handles missing data, sorting, grouping and statistics easily.
- Works with Matplotlib to make charts directly from data.
Frequently Asked Questions
What is the difference between a Series and a DataFrame?
A Series is one-dimensional – one column of values with an index. A DataFrame is two-dimensional – a table of rows and columns, where each column is a Series.
Why do we write import pandas as pd?
pd is a short alias, so we can write pd.Series() instead of pandas.Series(). It is a common convention.
How is a Series different from a NumPy array?
A Series has an index (labels) and aligns data by label in operations; a NumPy array uses only integer positions. A Series can also hold mixed or missing (NaN) values easily.
