Pandas How To Uncategorized How to create a dataframe

How to create a dataframe

Here’s a tutorial on how to create a dataframe in Python Pandas.

Dataframe is the basic form of data that is used in Pandas. So creating a data frame is the first problem you have to face when starting to work in Pandas.

In this tutorial you will learn how to create a dataframe in Pandas. I will show you several ways, and you can choose which one suits you.

How to create empty dataframe

Let’s start by creating an empty data frame.

import pandas as pd

my_df = pd.DataFrame()
 
print(my_df)

How to create a dataframe from list

This way you can create a dataframe from a list.

import pandas as pd
 
my_list = ['item1', 'item2', 'item3', 'item4']

my_df = pd.DataFrame(my_list)
print(my_df)

How to create dataframe from dictionary

This is the code that will open the dataframe from the dictionary.

import pandas as pd

my_dict = {'Index1': ['index1', 'index2', 'index3', 'index4'],
           'Values': [1, 2, 3, 4]}

my_df = pd.DataFrame(my_dict)

print(my_df)

I invite you to familiarize yourself with the contents of my Pandas How To page. You will find answers to many other problems you will encounter in your work with Pandas.

Further reading:
How to Sort Data Frame
How to merge two dataframes
How to save dataframe as text file
How to Create a 3D Pandas DataFrame

Tags:

2 thoughts on “How to create a dataframe”

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post