How to Create Data Frame From Dictionary of Series | Pandas Data Frame | Data Frame | Pandas

Опубликовано: 04 Октябрь 2024
на канале: Parag Dhawan
514
7

In Pandas, you can create a DataFrame from a dictionary of Series using the pd.DataFrame constructor. Here's an example:


import pandas as pd

Create a dictionary of Series
data = {'A': pd.Series([1, 2, 3]),
'B': pd.Series([4, 5, 6]),
'C': pd.Series([7, 8, 9])}

Create a DataFrame from the dictionary
df = pd.DataFrame(data)

Print the DataFrame
print(df)

In this example, we first create a dictionary data with three keys 'A', 'B', and 'C', each associated with a Series of values. The Series must have the same length. We then use the pd.DataFrame constructor to create a DataFrame df from the dictionary. The resulting DataFrame has three columns labeled 'A', 'B', and 'C', and three rows containing the values from the corresponding Series.

Alternatively, you can specify the index of the DataFrame using the index parameter of the pd.DataFrame constructor, like this:

Create a dictionary of Series with custom index
data = {'A': pd.Series([1, 2, 3], index=['x', 'y', 'z']),
'B': pd.Series([4, 5, 6], index=['x', 'y', 'z']),
'C': pd.Series([7, 8, 9], index=['x', 'y', 'z'])}

Create a DataFrame from the dictionary with custom index
df = pd.DataFrame(data, index=['x', 'y', 'z'])

Print the DataFrame
print(df)

In this example, we create a dictionary data with the same structure as before, but with custom index labels. We then use the pd.DataFrame constructor to create a DataFrame df from the dictionary, with the same custom index labels. The resulting DataFrame has three columns labeled 'A', 'B', and 'C', and three rows labeled 'x', 'y', and 'z', containing the values from the corresponding Series.

code snippet
create a DataFrame from Dictionary of Series

d = {'one':pd.Series([1,2,3],index=['a','b','c']),
'two':pd.Series([1,2,3,4],index = ['a','b','c','d'])}
df = pd.DataFrame(d)
print(df)

‪@ParagDhawan‬
=============================================================================
Link for Tutorial Series

Jupyter Notebook Tutorial Series:-
   • How To Open Jupyter Notebook in Windows  

Python Tutorial Series:-
   • Introduction to Python | Python Appli...  

Python Assignments and Objective Questions:-
   • Objective Questions Python - 1  

Tech. Videos By Parag Dhawan;-
   • Template Matching Using OpenCV (Pytho...  

Object-Oriented Programming in Python:-
   • How to Create Class and Object in Python  

File Handling in Python:-
   • How to Create a file in Python | Pyth...  

Exception Handling in Python:-
   • Exception Handling in Python  

NumPy Tutorial Series:-
   • NumPy  


=============================================================================
Feel free to connect and ask your queries:-

Linkedin:-   / parag-dhawan  
Youtube:-    / paragdhawan  
Facebook Page:- http://fb.me/dhawanparag
Instagram: -   / paragdhawan  
Twitter:-   / dhawan_parag  
GitHub:- https://github.com/paragdhawan/

=============================================================================
Show your support by Subscribing to the channel:-
https://www.youtube.com/c/ParagDhawan...
=============================================================================

#ParagDhawan
#Pandas
#DataScience
#DataAnalysis
#PandasTutorial
#PandasCourse
#Python3
#Python
#PythonProgramming

============================================================
How to Record Your Screen and make a tutorial video or demo video: -
   • How to Record Screen and Edit Video U...  
============================================================