Create a Pandas DataFrame from Lists (original) (raw)
Last Updated : 07 Oct, 2024
Converting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease.
**List to Dataframe Example
Simple list
data = [1, 2, 3, 4, 5]
Convert to DataFrame
df = pd.DataFrame(data, columns=['Numbers'])
Here we will discuss different ways to create a Pandas Dataframe from the lists:
Table of Content
- Using Dictionary
- Using zip()
- Using Datatype
- Using Multi-dimensional List
- With Index and Column Names
Create DataFrame from List using Dictionary
**Example 1: 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.
Python `
import pandas as pd
import pandas as pd
list of strings
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
Calling DataFrame constructor on list
df = pd.DataFrame(lst) print(df)
`
**Output:
0
0 Geeks
1 For
2 Geeks
3 is
4 portal
5 for
6 Geeks
**Example 2: 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.
Python `
importing pandas as pd
import pandas as pd
list of name, degree, score
nme = ["aparna", "pankaj", "sudhir", "Geeku"] deg = ["MBA", "BCA", "M.Tech", "MBA"] scr = [90, 40, 80, 98]
dictionary of lists
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
Convert List to Pandas Dataframe 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.
Python `
import pandas as pd
import pandas as pd
list of strings
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
list of int
lst2 = [11, 22, 33, 44, 55, 66, 77]
Calling DataFrame constructor after zipping
both lists, with columns specified
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 Datatype
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.
Python `
import pandas as pd
List1
lst = [['tom', 'reacher', 25], ['krish', 'pete', 30], ['nick', 'wilson', 26], ['juli', 'williams', 22]]
Create DataFrame
df = pd.DataFrame(lst, columns=['FName', 'LName', 'Age'])
Convert 'Age' column to float
df['Age'] = df['Age'].astype(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.
Python `
import pandas as pd
import pandas as pd
List1
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.
Python `
import pandas as pd
import pandas as pd
list of strings
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
Calling DataFrame constructor on list
with indices and columns specified
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