Python | Creating DataFrame from dict of narray/lists
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 few examples.
Code #1:
# Python code demonstrate creating # DataFrame from dict narray / lists # By default addresses. import pandas as pd # intialise data of lists. data = {'Category':['Array', 'Stack', 'Queue'], 'Marks':[20, 21, 19]} # Create DataFrame df = pd.DataFrame(data) # Print the output. print(df ) |
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.
Code #2:
# Python code demonstrate creating # DataFrame from dict narray / lists # By default addresses. import pandas as pd # intialise data of lists. data = {'Category':['Array', 'Stack', 'Queue'], 'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]} # Create DataFrame df = pd.DataFrame(data) # Print the output. print(df.transpose()) |
0 1 2
Category Array Stack Queue
Student_1 20 21 19
Student_2 15 20 14
Code #3: Providing index list to dataframe
# Python code demonstrate creating # DataFrame from dict narray / lists # By default addresses. import pandas as pd # intialise data of lists. data = {'Area':['Array', 'Stack', 'Queue'], 'Student_1':[20, 21, 19], 'Student_2':[15, 20, 14]} # Create DataFrame df = pd.DataFrame(data, index =['Cat_1', 'Cat_2', 'Cat_3']) # Print the output. print(df) |
Area Student_1 Student_2
Cat_1 Array 20 15
Cat_2 Stack 21 20
Cat_3 Queue 19 14
Recommended Posts:
- Python | Create a Pandas Dataframe from a dict of equal length lists
- Python | Creating a Pandas dataframe column based on a given condition
- Python | Convert a list of lists into tree-like dict
- Creating a Pandas DataFrame
- Creating a dataframe using Excel files
- Creating views on Pandas DataFrame
- Creating views on Pandas DataFrame | Set - 2
- Creating a dataframe from Pandas series
- Creating a Pandas dataframe using list of tuples
- Creating Pandas dataframe using list of lists
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- Python | Creating a 3D List
- Python | Creating a button in tkinter
- Creating linear kernel SVM in Python
- Creating a Proxy Webserver in Python | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



