0% found this document useful (0 votes)
53 views5 pages

Pandas Assignment 3

The document introduces Pandas, a Python library for data manipulation and analysis, highlighting its ability to handle structured data efficiently. It explains the concept of DataFrames, which are two-dimensional tables similar to spreadsheets, and provides various methods to create them from different data sources. Additionally, it covers the Pandas Series, a one-dimensional data structure that serves as a fundamental building block in Pandas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views5 pages

Pandas Assignment 3

The document introduces Pandas, a Python library for data manipulation and analysis, highlighting its ability to handle structured data efficiently. It explains the concept of DataFrames, which are two-dimensional tables similar to spreadsheets, and provides various methods to create them from different data sources. Additionally, it covers the Pandas Series, a one-dimensional data structure that serves as a fundamental building block in Pandas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment on Pandas

Introduction to Pandas

Pandas is a Python library that simplifies working with data in a structured, tabular format. It is
a powerful tool used for data cleaning, analysis, and managing large datasets with ease.

Code:

import pandas as pd​


print(pd.__version__)​

Output:

2.0.3 ​

Pandas DataFrame
A DataFrame is a two-dimensional table with labeled rows and columns, similar to a
spreadsheet. It can be seen as a collection of Series objects that all share the same index.

A DaaFrame can also be viewed as a specialized dictionary, where each column acts like a
Serie.

Code:

import pandas as pd​


df = pd.DataFrame({"City": ["Lahore", "Karachi", "Islamabad"], "Population": [11, 16, 1.1]})​
print(df)​

Output:

City Population​
0 Lahore 11.0​
1 Karachi 16.0​
2 Islamabad 1.1​
Constructing DataFrame Objects
There are several flexible ways to create a DataFrame.
●​ From a two-dimensional NumPy Array: You can build a DataFrame from a 2D NumPy
array with specified column names.​

Code:​

import numpy as np​
import pandas as pd​
arr = np.array([[10, 20], [30, 40]])​
df = pd.DataFrame(arr, columns=["A", "B"])​
print(df)​

Output:​
A B​
0 10 20​
1 30 40​

●​ From a dictionary of Series Objects: A DataFrame can be created from a dictionary


where each key-value pair is a column, and the values are Series objects8.​

Code:​

import pandas as pd​
s1 = pd.Series([90, 85], index=["Ahmed", "Fatima"])​
s2 = pd.Series([95, 88], index=["Ahmed", "Fatima"])​
df = pd.DataFrame({"Math": s1, "Science": s2})​
print(df)​

Output:​
Math Science​
Ahmed 90 95​
Fatima 85 88​

●​ From a List of Dictionaries: You can create a DataFrame from a list where each
dictionary represents a row.​

Code:​
data = [{"Name": "Zain", "Age": 25}, {"Name": "Ayesha", "Age": 28}]​
import pandas as pd​
df = pd.DataFrame(data)​
print(df)​

Output:​
Name Age​
0 Zain 25​
1 Ayesha 28​

●​ From a NumPy Structured Array: It's also possible to create a DataFrame from a
NumPy structured array.​

Code:​
import numpy as np​
import pandas as pd​
data = np.array([(101, "Ali"), (102, "Bilal")], dtype=[("ID", "i4"), ("Name", "U10")])​
df = pd.DataFrame(data)​
print(df)​

Output:​
ID Name​
0 101 Ali​
1 102 Bilal​

●​ From a Single Series Object: You can convert a single Series object into a DataFrame11.​

Code:​
import pandas as pd​
s = pd.Series([100, 200, 300], name="Scores")​
df = pd.DataFrame(s)​
print(df)​

Output:​
Scores​
0 100​
1 200​
2 300​

The Pandas Series Objec​


A Series is a one-dimensional data structure with labels, also known
as an index. It's the core building block of Pandas and works like a
more powerful NumPy array. The index acts like a key, and the
values are the data, similar to a dictionary.

Code:​
import pandas as pd​
s = pd.Series([50, 100, 150])​
print(s.values)​
print(s.index)​

Output:

[ 50 100 150 ]​
RangeIndex(start=0, stop=3, step=1)​

Creating Series Objects

You can construct a Series from various data types, including lists, NumPy arrays, dictionaries,
or even a single scalar value.

●​ From a list:​
Code:​
import pandas as pd​
s1 = pd.Series([5, 10, 15], index=["a", "b", "c"])​
print(s1)​

Output:​
a 5​
b 10​
c 15​
dtype: int64​

●​ From a dictionary:​
Code:​
import pandas as pd​
s2 = pd.Series({"apple": 10, "banana": 20})​
print(s2)​

Output:​
apple 10​
banana 20​
dtype: int64​

You might also like