Pandas is an open-source Python library. It provides us with some new data structures, such as Series and DataFrame. A pandas Series is a one-dimensional data structure. It is similar to NumPy one-dimensional array or a Python list. But, the main difference is it is labeled. And the pandas DataFrame is a two-dimensional data structure. It can contain tabular data. In this article, we will discuss how to create a Pandas DataFrame.
We can use the pandas.DataFrame() constructor to create a pandas DataFrame.
import pandas list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] df1 = pandas.DataFrame(list1) print(df1) df2 = pandas.DataFrame(list1, index=["Row 1", "Row 2", "Row 3"], columns=["Column 1", "Column 2", "Column 3"]) print(df2) dict1 = {“Column 1”: [1, 2, 3], "Column 2": [4, 5, 6], "Column 3": [7, 8, 9]} df3 = pandas.DataFrame(dict1) print(df3)
Here, list1 is a Python list that has three rows and three columns. We are using this list to create a DataFrame df1. So, df1 contains three rows and three columns. When we print df1, it shows the following:
0 1 2 0 1 2 3 1 4 5 6 2 7 8 9
Please note that pandas DataFrame is a …






0 Comments