DataFrames

A DataFrame is a two-dimensional, table-like structure made up of rows and columns, where every column is internally a Series sharing the same row index. It is the main structure you will use for almost all real-world data work in Pandas.

Creating a DataFrame

import pandas as pd

data = {
    "Name": ["Abhay", "Priya", "Rohit"],
    "City": ["Noida", "Delhi", "Gurugram"],
    "Score": [85, 92, 78]
}

df = pd.DataFrame(data)
print(df)
#      Name      City  Score
# 0   Abhay     Noida     85
# 1   Priya     Delhi     92
# 2   Rohit  Gurugram     78

Selecting Columns and Rows

print(df["Name"])          # a single column, returned as a Series
print(df[["Name", "Score"]])  # multiple columns, returned as a DataFrame

print(df.loc[1])           # row by label
print(df.iloc[0])          # row by position

Adding and Removing Columns

df["Passed"] = df["Score"] >= 80
df.drop("City", axis=1, inplace=True)
print(df)

Quick Structural Checks

print(df.shape)     # (rows, columns)
print(df.columns)   # column names
print(df.dtypes)    # data type of every column
Every column you pull out of a DataFrame with df["column_name"] is a genuine Series - so anything you already know about Series applies directly to a single column of a DataFrame.

Coming Up Next

Next, you'll learn how to bring data into a DataFrame from external files, and how to export your results back out again.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course