How to create Pandas DataFrame in Python

How to create Pandas DataFrame in Python | ninjasquad

A 2-dimensional labeled data structure like a table with rows and columns is what the Pandas DataFrame is. The dataframe’s size and values are mutable or changeable. It is the panda thing that is used the most. There are various ways to generate a Pandas DataFrame. Let’s go over each method for creating a DataFrame one at a time.

This article’s purpose is to explain panda’s data frames. We will discuss the syntax, the ideas behind a dataframe, and how to build a dataframe object. Additionally, you’ll see several examples of how to create a dataframe object with various data kinds.

Pandas dataframe

A pandas dataframe is an informational table with rows and columns of indexed data. A dataframe is used to help developers see data in a more controllable way. Big data management uses data frames more than any other industry because it makes working with massive datasets easier.

Wes McKinney developed the panda’s library in 2008 in response to the need for a tool for quantitative data analysis while working with data. Since its development, it has rapidly become one of the most well-liked data management solutions accessible through the Python programming libraries.

Now that we’ve covered its background let’s look at how to construct and manage data sets with pandas. We will concentrate on smaller subsets of data to demonstrate how you may use it in larger applications for your data needs.

Building a dataframe in Pandas

Pandas’ utilization requires clear visualization as a necessary component. Data visualization speeds up development and simplifies working with data by removing the hassle of unstructured data. Even well-structured data might be challenging to deal with without tables to visualize it.

In Pandas, a dataframe is created using the DataFrame() method. The dataframe creation syntax is as follows:

pandas.DataFrame(data, index, columns)


  • data: a dataset from which a dataframe is to be generated is the data. Lists, dictionaries, scalar values, series, ndarrays, etc., can all be used.
  • index: It’s optional; by default, the dataframe’s index runs from 0 to the last data value (n-1). The row label is clearly defined.
  • columns: This option is used to specify the dataframe’s column names. If the column name is not predefined, it will accept a value between 0 and n-1.

Developing a Blank DataFrame

# begin by importing the Pandas library for the creation of the DataFrame
import pandas as pd

# Making a blank dataframe and storing it in the df variable
df = pd.DataFrame()

# show the empty DataFrame by printing

A dataframe is made using pandas’ DataFrame() method. Thus, the dataframe in our example is called the df variable.

Building Dataframes from Lists

# start by importing the panda's library
import pandas as pd

# initialization of the list elements
vals = [27,37,47,57,67,77]

# Creation of the pandas DataFrame with column name is explicitly provided
df = pd.DataFrame(vals, columns=['Numbers'])

# print dataframe.

Creating a Pandas DataFrame from lists of lists

# Import pandas library
import pandas as pd

# initialization of a list of lists
data = [['ken', 13], ['angelo', 18], ['green', 17]]

# Creation of a pandas DataFrame
df = pd.DataFrame(data, columns=['f_name', 'age'])

# show the dataframe by using the print

Building a DataFrame from a dict of lists and narray/lists

The length of each narray must be the same to generate a DataFrame from a dict of narray/list. If the index is supplied, the length index must match the length of the arrays. If no index is provided, range(n), where n is the array’s length, will be used as the default index.

# Python code is used to create a DataFrame from a list or dict - standard addresses

import pandas as pd

# initialization of lists' data.
info = {'f_name': ['Mike', 'Kean', 'Grealish', 'Holand'],
		'Age': [27, 28, 26, 25]}

# creating a DataFrame
info_df = pd.DataFrame(info)

# display the output.

When constructing a dataframe using a dictionary, the dictionary’s default keys are the column names. Using the column argument, we can also directly supply the column name.

Creating a DataFrame by explicitly proving the index label

# Making a pandas DataFrame with index by using Python code

# DataFrame using arrays.
import pandas as pd

# initialization of lists' data
user_info = {'f_name': ['Mike', 'John', 'James', 'Ken'],
		'balance': [909, 780, 495, 390]}

# Creation of pandas DataFrame.
df = pd.DataFrame(user_info, index=['pos_1','pos_2','pos_3','pos_4'])

# print the data

Dataframe creation from a list of dicts

You can generate a Pandas DataFrame by providing lists of dictionaries as input data. Dictionary keys will, by default, be treated as columns.

# Example of Python code shows how to build Pandas DataFrames from lists of dicts.
import pandas as pd

# Initialize data to lists.
list_of_dicts = [{'x': 11, 'y': 12, 'q': 13},
		{'x': 20, 'y': 30, 'q': 40}]

# Creation of a  DataFrame.
df = pd.DataFrame(list_of_dicts )

# Printing  of the data

The creation of pandas dataframe using lists of dictionaries and row indexes is demonstrated in another example.

# Create a Pandas DataFrame using Python code by supplying lists of dictionaries and row indices.
import pandas as pd

# Initialize data of lists
vals = [{'y': 4, 'q': 5}, {'x': 15, 'y': 25, 'q': 35}]

# Giving a list of dictionaries and a row index creates a pandas DataFrame.
df = pd.DataFrame(vals, index=['one', 'two'])

# Print the data

Another example of creating pandas DataFrame with both a row index and a column index from lists of dictionaries.

# A Pandas DataFrame containing lists of dictionaries and row and column indexes is created using Python code.

import pandas as pd

# Initialization of the lists data.
vals = [{'x': 3, 'y': 4},
		{'x': 10, 'y': 15, 'q': 25}]

# Values with two-column indices are identical to dictionary keys.
first_df = pd.DataFrame(vals, index=['one', 'two'],
				columns=['x', 'y'])

# one index, two column indices, and a distinct name
second_df = pd.DataFrame(data, index=['one','two'],
				columns=['x', 'y1'])

# for the first data frame, print the outcome
print(first_df, "\n")

# for the second data frame, print the results

Using the zip() function, create a DataFrame

The list(zip()) function allows two lists to be combined. Call the pd.DataFrame() function to construct the pandas DataFrame.

#Python program to show how to create a pandas Datadaframe using zip from a list.

import pandas as pd

# list one
f_name = ['mike', 'angelo', 'white', 'kean']

# list two
age = [22, 28, 24, 20]

#get the tuple list from two lists, then combine them using zip().
tuples_list = list(zip(f_name, age))

# show tuples' data

# Conversion of lists of tuples into a corresponding pandas Dataframe.
df = pd.DataFrame(tuples_list,columns=['f_name', 'age'])

# Print data.

Making a dataframe out of a series

Series must be passed as an input to the DataFrame() function to generate a dataframe from them.

# Using Python code, a Pandas Dataframe is created from a series.

import pandas as pd

# Initialization of the provided data to series.
vals = pd.Series([13, 23, 33, 43])
# creation of the Dataframe.
df = pd.DataFrame(vals)

# printing the data.

Generating a DataFrame from a series of dictionary

Dictionary can be provided to create a DataFrame from a Dict of Series. The union of all the series of passed indices makes up the resultant index.

# Using Python code, a Pandas Dataframe is created from a dictionary of series.

import pandas as pd

# Create series-specific Dicts for your data.
vals = {'first': pd.Series([17, 27, 37, 47], index=['x', 'y', 'q', 'w']),
	'second': pd.Series([17, 27, 37, 47], index=['x', 'y', 'q', 'w'])}

# creating a Dataframe.
series_df = pd.DataFrame(vals)

# print the data.

Example: Using the list of dictionaries to create a pandas dataframe

Each dictionary in the following code represents a single row, and the keys stand in for the names of the columns.

# start by importing the library pandas
import pandas as pd

# Creation of a list of dictionaries
employee_list = [
   {'employee_name': 'John', 'department': 'Operations', 'Rating': 3.1},
   {'employee_name': 'James', 'department': 'Marketing', 'Rating': 3.3},
   {'employee_name': 'Mike', 'department': 'Sales', 'Rating': 2.8},
   {'employee_name': 'Angelo', 'department': 'Information Technology', 'Rating': 4.0}]

# Make a DataFrame.
dframe = pd.DataFrame(employee_list)

Example: Using the zip() function, create a Pandas DataFrame

The list(zip()) function allows for merging various lists. The function pd.DataFrame() is called to build a pandas DataFrame in the example below. The result is the creation of three combined lists known as tuples.

import pandas as pd
# first list
employee_name = ['James', 'Angelo', 'Joy', 'White']
# second list
department_name = ['operations', 'marketing', 'Sales', 'Information Technology']
# third list
rating = [3.1, 3.3, 2.8, 4.0]
# Continue by merging the list of tuples from the three lists using zip().
emp_tuple = list(zip(employee_name, department, rating))

# Assign data values to tuples.

# Conversion of tuples list into a pandas Dataframe.
dframe = pd.DataFrame(tuples, columns=['employee_name', 'department', 'rating'])
# Print data.


A 2D (two-dimensional) annotated data structure called a Pandas DataFrame displays data in a tabular format with distinct rows and columns. The DataFrame functions like a spreadsheet with three distinct parts: an index, columns, and data. It makes it simpler to grasp. The most popular approach to using Pandas objects is through DataFrames.

Different techniques can be used to produce Pandas DataFrames. This article has covered every approach conceivable for creating Pandas DataFrames in Python. All examples have been tested using the PyCharm IDE. You are free to test with your favorite tool as you so, please.

Source: Internet

Leave a Comment

We are offering free coding tuts