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.
Python3
import pandas as pd
lst = [[ 'Geek' , 25 ], [ 'is' , 30 ],
[ 'for' , 26 ], [ 'Geeksforgeeks' , 22 ]]
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:
Python3
import pandas as pd
data = [[ 'Geek1' , 28 , 'Analyst' ],
[ 'Geek2' , 35 , 'Manager' ],
[ 'Geek3' , 29 , 'Developer' ]]
columns = [ 'Name' , 'Age' , 'Occupation' ]
df = pd.DataFrame.from_records(data, columns = columns)
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.
Python3
import pandas as pd
data = [[ 'Geek1' , 26 , 'Scientist' ],
[ 'Geek2' , 31 , 'Researcher' ],
[ 'Geek3' , 24 , 'Engineer' ]]
columns = [ 'Name' , 'Age' , 'Occupation' ]
df = pd.DataFrame.from_dict( dict ( zip (columns, zip ( * data))))
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’. The specified data types for all columns are set to float using the dtype
parameter.
Python3
import pandas as pd
data = [[ 'Geek1' , 'Reacher' , 25 ],
[ 'Geek2' , 'Pete' , 30 ],
[ 'Geek3' , 'Wilson' , 26 ],
[ 'Geek4' , 'Williams' , 22 ]]
columns = [ 'FName' , 'LName' , 'Age' ]
df = pd.DataFrame(data, columns = columns, dtype = float )
print (df)
|
Output :
FName LName Age
0 Geek1 Reacher 25.0
1 Geek2 Pete 30.0
2 Geek3 Wilson 26.0
3 Geek4 Williams 22.0
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Nov, 2023
Like Article
Save Article