How to Create a Pandas DataFrame in Python

In this guide, you’ll see two practical methods to create a Pandas DataFrame in Python:

  • By typing the values directly in Python
  • By importing values from a file (such as a CSV file) and creating the DataFrame from that data

Both methods are commonly used depending on whether your data is manually defined or stored externally.

Method 1: Typing the Values Directly in Python

To create a Pandas DataFrame manually, you can use the following general structure:

import pandas as pd

data = {
    'first_column': ['value1', 'value2'],
    'second_column': ['value1', 'value2']
}

df = pd.DataFrame(data)

print(df)

Note that numeric values do not require quotes unless you intentionally want them stored as strings.

Example

Assume you have the following product data:

product_nameprice
laptop1250
printer180
tablet350
desk500
chair220

You can create the DataFrame as follows:

import pandas as pd

data = {
    'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
    'price': [1250, 180, 350, 500, 220]
}

df = pd.DataFrame(data)

print(df)

Output:

  product_name  price
0       laptop   1250
1      printer    180
2       tablet    350
3         desk    500
4        chair    220

By default, Pandas assigns a numeric index starting from 0.

Assigning a Custom Index

You can also define your own row labels:

import pandas as pd

data = {
    'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
    'price': [1250, 180, 350, 500, 220]
}

df = pd.DataFrame(
    data,
    index=['item_1', 'item_2', 'item_3', 'item_4', 'item_5']
)

print(df)

Output:

         product_name  price
item_1         laptop   1250
item_2        printer    180
item_3         tablet    350
item_4           desk    500
item_5          chair    220

Now the DataFrame uses your custom index instead of the default numeric index.

Method 2: Importing Values from a CSV File

In real-world projects, data is often stored in files. You can import a CSV file and create a DataFrame from it.

Use the following template:

import pandas as pd

data = pd.read_csv(r'Full\Path\To\File\file_name.csv')
df = pd.DataFrame(data)

print(df)

Example

Suppose you have a CSV file named product_list.csv containing:

product_nameprice
laptop1250
printer180
tablet350
desk500
chair220

Assume the file is stored at:

C:\Users\Alex\Documents\product_list.csv

Here is the complete Python script:

import pandas as pd

data = pd.read_csv(r'C:\Users\Alex\Documents\product_list.csv')
df = pd.DataFrame(data)

print(df)

You will get the same DataFrame as before:

  product_name  price
0       laptop   1250
1      printer    180
2       tablet    350
3         desk    500
4        chair    220

The letter r before the path ensures that special characters such as backslashes (\) are handled correctly.

You can also import data from Excel files using read_excel() in a similar way.

Finding the Maximum Value in the DataFrame

Once your data is inside a DataFrame, you can perform various operations such as filtering, sorting, and statistical calculations.

For example, to find the maximum price:

import pandas as pd

data = {
    'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
    'price': [1250, 180, 350, 500, 220]
}

df = pd.DataFrame(data)

max_price = df['price'].max()

print(max_price)

Output:

1250

The .max() function returns the highest value in the selected column.

Conclusion

You have seen two main ways to create a Pandas DataFrame:

  • Manually defining the data inside Python
  • Importing the data from a CSV file

Once the DataFrame is created, you can easily perform calculations, filter data, analyze trends, and manipulate datasets efficiently. Understanding how to properly create a DataFrame is the first essential step toward working confidently with data in Python.

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments