0
0
Pandasdata~5 mins

DataFrame as labeled two-dimensional table in Pandas

Choose your learning style9 modes available
Introduction

A DataFrame is like a table with rows and columns that have names. It helps you organize and look at data easily.

When you want to store data with rows and columns, like a spreadsheet.
When you need to label your data so you can find things by name, not just position.
When you want to do calculations or summaries on data organized in a table.
When you want to combine different types of data (numbers, text) in one place.
When you want to read or write data from files like CSV or Excel.
Syntax
Pandas
import pandas as pd

df = pd.DataFrame(data, index=index_labels, columns=column_labels)

data can be a list, dictionary, or array of values.

index labels the rows, and columns labels the columns.

Examples
Create a DataFrame from a dictionary. Columns are 'Name' and 'Age'. Rows get default numbers 0, 1.
Pandas
import pandas as pd

data = {'Name': ['Anna', 'Bob'], 'Age': [28, 34]}
df = pd.DataFrame(data)
Create a DataFrame from a list of lists with custom row and column labels.
Pandas
import pandas as pd

data = [[10, 20], [30, 40]]
columns = ['Math', 'Science']
index = ['Alice', 'Bob']
df = pd.DataFrame(data, columns=columns, index=index)
Sample Program

This program creates a DataFrame with student names, their grades, and if they passed. Then it prints the table.

Pandas
import pandas as pd

# Create data as a dictionary
student_data = {
    'Name': ['John', 'Emma', 'Sophia'],
    'Grade': [88, 92, 95],
    'Passed': [True, True, True]
}

# Create DataFrame
df = pd.DataFrame(student_data)

# Print the DataFrame
print(df)
OutputSuccess
Important Notes

DataFrames can hold different types of data in each column, like numbers and text.

Row and column labels help you find and select data easily.

You can think of a DataFrame like a spreadsheet or a simple database table.

Summary

A DataFrame is a table with named rows and columns.

It helps organize data clearly and makes analysis easier.

You can create DataFrames from many data types like lists or dictionaries.