Python for Data Science: Chapter 5: NumPy and Pandas Libraries

Pandas: Exploring Data using Data Frames

Python library

A DataFrame is a multi‒dimensional data structure in which data is arranged in the form of rows and columns.

Data Structures

• In Pandas, there are two important data structures that are used –

1) Series and

2) Data frames.

Let us discuss them in detail

 

Exploring Data using Data Frames

• A DataFrame is a multi‒dimensional data structure in which data is arranged in the form of rows and columns. Series is like a column and DataFrame is the whole table.

Syntax

pandas.DataFrame( data, index, columns, dtype, copy)

Parameters:

• data(required): Input data, can be ndarray, series, map, lists, dict, constants and another DataFrame.

•  index(optional): For labeling rows.

•  columns(Optional): For labeling columns.

dtype(Optional): Data type of each column.

copy(Optional): This makes a copy of the input data.

For example ‒

In [1]: import pandas as pd

a = pd.DataFrame()

print(a)

Empty DataFrame

Columns: [ ]

Index: [ ]

• By the above code, we can create an empty DataFrame because no value is passed to DataFrame.

A simple DataFrame can be created using a list. For example ‒

In [2]: import pandas as pd

a = [10,20,30,40,50]

X = pd.DataFrame(a)

print(x)

       0

0    10

1    20

2    30

3    40

4    50

We get output as our data, column index '0', as only 1‒column and by default, row index starts from '0'.

Creation of DataFrame with label to column

Following is an illustration in which we can give the label to each column in the dataframe.

In [3]: import pandas as pd

data = {

"name": ['AAA', 'BBB', 'CCC'],

"age":[20,24,22]

}

x = pd.DataFrame(data)

print(x)

     name        age

0   AAA         20

1   BBB         24

2   CCC         22

Accessing specific row in DataFrame

We can access specific rows in a dataframe using loc attributes. For example ‒

In [7]: import pandas as pd

data = {

"name": [ 'AAA', 'BBB', 'CCC'],

"age":[20,24,22]

}

x = pd.DataFrame(data)

print(x)

print("The row at index 1 is...")

print(x.loc[1])

        name    age

0     AAA      20   

1     BBB      24

2    CCC       22

The row at index 1 is...

name   BBB

age      24

Name: 1, dtype: object

Naming the index

Instead of default index 0,1,2,... we can name the index as follows ‒

In [8]: import pandas as pd

data = {

"name": [ 'AAA', 'BBB', 'CCC'],

'age":[20,24,22]

}

x = pd.DataFrame(data, index = ["one", "two", "three"])

print(x)

         name     age

one      AAA   20

two      BBB    24

three    CCC    22

 

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - Pandas: Exploring Data using Data Frames


Python for Data Science: Chapter 5: NumPy and Pandas Libraries



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation