Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Creating a Pandas Dataframe can be achieved in multiple ways. Let’s see how we can create a Pandas DataFrame from Lists.
Creating Pandas DataFrames from Lists
Converting lists to DataFrames is crucial in data analysis, enabling you to perform sophisticated data manipulations and analyses with ease.
Here we will discuss different ways to create a Pandas Dataframe from the lists:
- Create Dataframe from List using Constructer
- Create DataFrame from List using Dictionary
- Create DataFrame from list using zip()
- Create DataFrame from list by changing data type
- Create DataFrame from list using multi-dimensional list
- Create DataFrame from List with index and column names
Create Dataframe from List using Constructer
To convert a list to a Pandas DataFrame, you can use the pd.DataFrame()
constructor. This function takes a list as input and creates a DataFrame with the same number of rows and columns as the input list.
Python3
import pandas as pd
lst = [ 'Geeks' , 'For' , 'Geeks' , 'is' ,
'portal' , 'for' , 'Geeks' ]
df = pd.DataFrame(lst)
print (df)
|
Output:
0
0 Geeks
1 For
2 Geeks
3 is
4 portal
5 for
6 Geeks
Create DataFrame from List using Dictionary
To use lists in a dictionary to create a Pandas DataFrame, we Create a dictionary of lists and then Pass the dictionary to the pd.DataFrame()
constructor. Optionally, we can specify the column names for the DataFrame by passing a list of strings to the columns
parameter of the pd.DataFrame()
constructor.
Python3
import pandas as pd
nme = [ "aparna" , "pankaj" , "sudhir" , "Geeku" ]
deg = [ "MBA" , "BCA" , "M.Tech" , "MBA" ]
scr = [ 90 , 40 , 80 , 98 ]
dict = { 'name' : nme, 'degree' : deg, 'score' : scr}
df = pd.DataFrame( dict )
print (df)
|
Output:
name degree score
0 aparna MBA 90
1 pankaj BCA 40
2 sudhir M.Tech 80
3 Geeku MBA 98
Create DataFrame from List using zip()
To create a Pandas DataFrame from lists using zip(). We can also use the zip()
function to zip together multiple lists to create a DataFrame with more columns.
Python3
import pandas as pd
lst = [ 'Geeks' , 'For' , 'Geeks' , 'is' , 'portal' , 'for' , 'Geeks' ]
lst2 = [ 11 , 22 , 33 , 44 , 55 , 66 , 77 ]
df = pd.DataFrame( list ( zip (lst, lst2)),
columns = [ 'Name' , 'val' ])
print (df)
|
Output:
Name val
0 Geeks 11
1 For 22
2 Geeks 33
3 is 44
4 portal 55
5 for 66
6 Geeks 77
Create DataFrame from List by changing data type
To create a Pandas DataFrame using a multi-dimensional list with column names and dtypes specified. By specifying dtypes, we can ensure that the DataFrame is created with the correct data types.
Python3
import pandas as pd
lst = [[ 'tom' , 'reacher' , 25 ], [ 'krish' , 'pete' , 30 ],
[ 'nick' , 'wilson' , 26 ], [ 'juli' , 'williams' , 22 ]]
df = pd.DataFrame(lst, columns = [ 'FName' , 'LName' , 'Age' ], dtype = float )
print (df)
|
Output:
FName LName Age
0 tom reacher 25.0
1 krish pete 30.0
2 nick wilson 26.0
3 juli williams 22.0
Create DataFrame from List using multi-dimensional list
To create a DataFrame using a multi-dimensional list, you can use the pd.DataFrame()
constructor. The pd.DataFrame()
constructor takes a list of lists as input and creates a DataFrame with the same number of rows and columns as the input list.
Python3
import pandas as pd
lst = [[ 'tom' , 25 ], [ 'krish' , 30 ],
[ 'nick' , 26 ], [ 'juli' , 22 ]]
df = pd.DataFrame(lst, columns = [ 'Name' , 'Age' ])
print (df)
|
Output:
Name Age
0 tom 25
1 krish 30
2 nick 26
3 juli 22
Create DataFrame from List with index and column names
To create a DataFrame using a list with index and column names, you can use the pd.DataFrame()
constructor with the index
and columns
parameters.
Python3
import pandas as pd
lst = [ 'Geeks' , 'For' , 'Geeks' , 'is' , 'portal' , 'for' , 'Geeks' ]
df = pd.DataFrame(lst, index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' ],
columns = [ 'Names' ])
print (df)
|
Output:
Names
a Geeks
b For
c Geeks
d is
e portal
f for
g Geeks
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 :
06 Dec, 2023
Like Article
Save Article