Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐼 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.
The data-science trio
Collect data (CSV / MySQL)Pandas: clean & analyseMatplotlib: visualiseDecision
Where each library fits

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
Series marks
values
Asha78
Ravi92
Neha65
Index on the left, values on the right

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 ndarrayPandas Series
Accessed by integer position onlyAccessed by label or position
Operations match elements by positionOperations match elements by index label
Missing values are not naturalMissing values shown as NaN
Faster for pure mathsEasier 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.
← Class 12 Informatics Practices
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicSeries: Create, Index & Slice Your progress: 6 / 25 (24%) · Lesson 7 / 25 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free