How to Create or Initialize a Pandas DataFrame? Examples
Pandas - Create or Initialize DataFrame
In Python Pandas module, DataFrame is a very basic and important type. To create a DataFrame from different sources of data or other Python datatypes, we can use DataFrame() constructor.
In this tutorial, we will learn different ways of how to create and initialize Pandas DataFrame.
Syntax of DataFrame()
The syntax of DataFrame() class is
DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
where all the arguments are optional and
- data can be ndarray, iterable, dictionary or another dataframe.
- index can be Index or an array. If no index is provided, it defaults to Range Index, i.e., 0 to number of rows - 1.
- columns are used to label the columns
- dtype is used to specify or force a datatype on the data. If you do not specify, dtype is inferred from the data itself.
- copy if True, copies data from inputs. Note that this affects only DataFrame or 2d ndarray input.
Video Tutorial
Examples
1. Create an empty DataFrame
To create an empty DataFrame, pass no arguments to pandas.DataFrame() class.
In this example, we create an empty DataFrame and print it to the console output.
Python Program
import pandas as pd
df = pd.DataFrame()
print(df)
Explanation
- The program imports the
pandas
library, which is used for data manipulation and analysis. - An empty dataframe
df
is created using thepd.DataFrame()
function. - The
print(df)
statement displays the empty dataframedf
to the console.
Output
Empty DataFrame
Columns: []
Index: []
2. Create DataFrame from List of Lists
To initialize a DataFrame from list of lists, you can pass this list of lists to pandas.DataFrame() constructor as data
argument.
In this example, we will create a DataFrame for list of lists. Each inner list represents a row in DataFrame.
Python Program
import pandas as pd
#list of lists
data = [['a1', 'b1', 'c1'],
['a2', 'b2', 'c2'],
['a3', 'b3', 'c3']]
df = pd.DataFrame(data)
print(df)
Explanation
- The program imports the
pandas
library, which is used for data manipulation and analysis. - A list of lists
data
is created, where each inner list represents a row in the dataframe. - The
pd.DataFrame()
function is used to convert the list of listsdata
into a dataframedf
. - The
print(df)
statement is used to display the contents of the dataframedf
to the console.
Output
0 1 2
0 a1 b1 c1
1 a2 b2 c2
2 a3 b3 c3
3. Create DataFrame from Dictionary
To initialize a DataFrame from dictionary, pass this dictionary to pandas.DataFrame() constructor as data
argument.
In this example, we will create a DataFrame for a dictionary. Each key:value pair in the dictionary represents column_name:column_data in the DataFrame.
Python Program
import pandas as pd
#dictionary
data = {'col1': ['a1', 'a2', 'a3'],
'col2': ['b1', 'b2', 'b3'],
'col3': ['c1', 'c2', 'c3']}
df = pd.DataFrame(data)
print(df)
Explanation
- The program imports the
pandas
library, which is used for data manipulation and analysis. - A dictionary
data
is created with keys representing column names ('col1', 'col2', 'col3') and values representing the data for each column. - The
pd.DataFrame()
function is used to convert the dictionarydata
into a dataframedf
. - The
print(df)
statement is used to display the contents of the dataframedf
to the console.
Output
col1 col2 col3
0 a1 b1 c1
1 a2 b2 c2
2 a3 b3 c3
Summary
In this Pandas Tutorial, we learned how to create an empty DataFrame, and then to create a DataFrame with data from different Python objects, with the help of well-detailed examples.