Make a Pandas DataFrame with two-dimensional list | Python
In this discussion, we will illustrate the process of creating a Pandas DataFrame with the two-dimensional list. Python is widely recognized for its effectiveness in data analysis, thanks to its robust ecosystem of data-centric packages. Among these packages, Pandas stands out, streamlining the import and analysis of data. There are various methods to achieve a Pandas DataFrame, and in this article, we will focus on creating one using a two-dimensional list.
Pandas DataFrame with Two-dimensional List
There are several methods for creating a Pandas DataFrame with the two-dimensional list. In this context, we will explain some commonly used approaches.
- Using
pd.DataFrame()
- Using
pd.DataFrame.from_records()
- Using
pd.DataFrame.from_dict()
- Using Specifying Data Types
Create Pandas Dataframe from 2D List using pd.DataFrame()
In this example below code creates a Pandas DataFrame (‘df’) from a two-dimensional list (‘lst’) with specified column names (‘Tag’ and ‘number’) and prints the resulting DataFrame.
# import pandas as pd
import pandas as pd
# List1
lst = [['Geek', 25], ['is', 30],
['for', 26], ['Geeksforgeeks', 22]]
# creating df object with columns specified
df = pd.DataFrame(lst, columns =['Tag', 'number'])
print(df )
Output :
Tag number
0 Geek 25
1 is 30
2 for 26
3 Geeksforgeeks 22
Create Pandas Dataframe from 2D List using pd.DataFrame.from_records()
In this example below code uses the pandas library in Python to create a DataFrame from a two-dimensional list (data
). The DataFrame has columns with names ‘Name’, ‘Age’, and ‘Occupation’. The print(df)
statement will display the DataFrame. Here’s the expected output:
import pandas as pd
# Two-dimensional list
data = [['Geek1', 28, 'Analyst'],
['Geek2', 35, 'Manager'],
['Geek3', 29, 'Developer']]
# Column names
columns = ['Name', 'Age', 'Occupation']
# Creating DataFrame using pd.DataFrame.from_records()
df = pd.DataFrame.from_records(data, columns=columns)
# Displaying the DataFrame
print(df)
Output:
Name Age Occupation
0 Geek1 28 Analyst
1 Geek2 35 Manager
2 Geek3 29 Developer
Create Pandas Dataframe from 2D List using pd.DataFrame.from_dict()
In this example below code uses the pandas library in Python to create a DataFrame from a two-dimensional list (data
). Instead of using pd.DataFrame.from_records()
, this time it uses pd.DataFrame.from_dict()
along with the zip
function to transpose the data.
import pandas as pd
# Two-dimensional list
data = [['Geek1', 26, 'Scientist'],
['Geek2', 31, 'Researcher'],
['Geek3', 24, 'Engineer']]
# Column names
columns = ['Name', 'Age', 'Occupation']
# Creating DataFrame using pd.DataFrame.from_dict()
df = pd.DataFrame.from_dict(dict(zip(columns, zip(*data))))
# Displaying the DataFrame
print(df)
Output:
Name Age Occupation
0 Geek1 26 Scientist
1 Geek2 31 Researcher
2 Geek3 24 Engineer
Create Pandas Dataframe from 2D List using Specifying Data Types
In this example below code uses the pandas library in Python to create a DataFrame from a two-dimensional list (data
). The DataFrame has columns with names ‘FName’, ‘LName’, and ‘Age’.
import pandas as pd
# Two-dimensional list
data = [['Geek1', 'Reacher', 25],
['Geek2', 'Pete', 30],
['Geek3', 'Wilson', 26],
['Geek4', 'Williams', 22]]
# Column names
columns = ['FName', 'LName', 'Age']
# Creating DataFrame with specified data types
df = pd.DataFrame(data, columns=columns)
# Displaying the DataFrame
print(df)
Output :
FName LName Age
0 Geek1 Reacher 25
1 Geek2 Pete 30
2 Geek3 Wilson 26
3 Geek4 Williams 22