Different ways to create pandas DataFrame from dictionary, list of lists, list of dictionaries
In this pandas tutorial, we will discuss about:
-
different ways to create pandas dataframe,
-
create pandas dataframe from dictionary,
-
create pandas dataframe from list of lists,
-
create pandas dataframe from list of dictionaries,
Lets learn about different ways to create pandas dataframe.
DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column. So it will store the data in rows and columns.
We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Method 1 - create pandas dataframe from dictionary
Given below is syntax for create pandas dataframe from dictionary.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe.
The data can be a dictionary that stores list of values with specified key
Syntax:
pandas.DataFrame({'column1':[value1,value2,.,value n],...........,'column n':[value1,value2,.,value n]})
where,
1. column1....column n refers to the column name
2. values refers to the values/rows
Example: create pandas dataframe from dictionary
In this create pandas dataframe from dictionary example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: Here we create pandas dataframe from dictionary
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Method 2 - create pandas dataframe from list of lists
Here we will learn to create pandas dataframe from list of lists.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe.
The data can be a list of lists or nested list.
Syntax:
pandas.DataFrame([[value1,.value n],................,[value1,.value n],columns=[list_of_columns])
where,
1. column1....column n refers to the column name
2. values refers to the values/rows
3. we can pass column names in a list through columns parameter.
Example: create pandas dataframe from list of lists
In this create pandas dataframe from list of lists example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame([['c-001',"vignan university","guntur",'1200'],
['c-021',"vvit","guntur",'3422'],
['c-002',"RVR - JC","guntur",'5644'],
['c-004',"Andhra University","guntur",'670']
],columns=['college_id','college_name','college_address','Total Staff'],index=['one','two','three','four'])
# display
print(data)
Output: Result of create pandas dataframe from list of lists is below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Method 3 - create pandas dataframe from list of dictionaries
Here we will learn to create pandas dataframe from list of dictionaries.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe,
The data can be a list of dictionaries.
Syntax:
pandas.DataFrame([{'key':value,........'key':value n},................,{'key':value,........'key':value n})
where,
1. column1....column n refers to the column name
2. values refers to the values/rows
3. key will be the column name.
Example: create pandas dataframe from list of dictionaries
In this create pandas dataframe from list of dictionaries example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame([{'college_id':'c-001','college_name':"vignan university",'college_address':"guntur",'Total Staff':'1200'},
{'college_id':'c-021','college_name':"vvit",'college_address':"guntur",'Total Staff':'3422'},
{'college_id':'c-002','college_name':"RVR - JC",'college_address':"guntur",'Total Staff':'5644'},
{'college_id':'c-004','college_name':"Andhra University",'college_address':"guntur",'Total Staff':'670'}
],index=['one','two','three','four'])
# display
print(data)
Output: create pandas dataframe from list of dictionaries result
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Thus we have learned how to create pandas dataframe from dictionary, create pandas dataframe from list of lists, create pandas dataframe from list of dictionaries.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 20,2022