DataFramepandas is a data analysis software package in Python, which is used for manipulating data. This library contains the DataFrame class.
A pandas DataFrame is a 2D data structure, which is organized in rows and columns in a tabular format. It allows us to store tabular data and perform various operations on that data using different methods.
DataFrame from a Python listA Python list is an ordered and changeable collection of data objects. We can create a pandas DataFrame from a Python list. Let's look at the following code to see how we can achieve this.
The code snippet given below demonstrates the creation of a pandas DataFrame from a Python list.
df = pandas.DataFrame(list)
pandas: This is the library that contains the DataFrame class. The line shown above calls the constructor for the DataFrame class.list: This is a required parameter that needs to be passed to the constructor.The DataFrame constructor returns a pandas DataFrame that contains all the elements in a list. In the code snippet given above, the DataFrame instance is stored in df.
Let's look at an example that uses the syntax shown above:
# Importing pandasimport pandas as pd# creating a listls = [1,2,3,4,5]#creating dataframe from listdf = pd.DataFrame(ls)# printing dfprint(df)# creating a listls = [[1,2,3], [4,5,6], [7,8,9]]#creating dataframe from listdf = pd.DataFrame(ls)# printing dfprint(df)
pandas as pd.ls.DataFrame called df from the ls list.df DataFrame. We observe that df contains all the elements from ls.ls to make it a list that contains other lists.DataFrame called df from the ls list.df DataFrame. We observe that df contains all the elements from ls.Free Resources