As we know Pandas is all-time great tools for data analysis. One of the most important data type is dataframe. It is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used pandas object.
Pandas DataFrame can be created in multiple ways. Let’s discuss how to create Pandas dataframe using dictionary of ndarray (or lists). Let’s try to understand it better with a few examples.
Using Pandas DataFrame to create Dataframe from List
Example #1: We will use pd.DataFrame() function to create the dataframe from the list.
Python3
import pandas as pd
data = { 'Category' :[ 'Array' , 'Stack' , 'Queue' ],
'Marks' :[ 20 , 21 , 19 ]}
df = pd.DataFrame(data)
print (df)
|
Output:
Category Marks
0 Array 20
1 Stack 21
2 Queue 19
Note: To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.
Example #2:
Python3
import pandas as pd
data = { 'Category' :[ 'Array' , 'Stack' , 'Queue' ],
'Student_1' :[ 20 , 21 , 19 ], 'Student_2' :[ 15 , 20 , 14 ]}
df = pd.DataFrame(data)
print (df.transpose())
|
Output:
0 1 2
Category Array Stack Queue
Student_1 20 21 19
Student_2 15 20 14
Using Pandas Dataframe with the index parameter
Example #1: Providing index list to dataframe
Python3
import pandas as pd
data = { 'Area' :[ 'Array' , 'Stack' , 'Queue' ],
'Student_1' :[ 20 , 21 , 19 ], 'Student_2' :[ 15 , 20 , 14 ]}
df = pd.DataFrame(data, index = [ 'Cat_1' , 'Cat_2' , 'Cat_3' ])
print (df)
|
Output:
Area Student_1 Student_2
Cat_1 Array 20 15
Cat_2 Stack 21 20
Cat_3 Queue 19 14
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 :
25 Aug, 2023
Like Article
Save Article