How to Create a Data Frame Using Dictionary | Python Data Frame | Pandas Tutorial | Pandas

preview_player
Показать описание
In Pandas, you can create a DataFrame from a dictionary using the pd.DataFrame constructor. Here's an example:

import pandas as pd

# Create a dictionary
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [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', and their corresponding values as lists. The keys represent the column labels of the DataFrame, and the values represent the row values. 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 lists.

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

# Create a dictionary with custom row index
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}

# Create a DataFrame from the dictionary with custom row 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 no index. We then use the pd.DataFrame constructor to create a DataFrame df from the dictionary, with a custom row index specified as a list of strings. The resulting DataFrame has three columns labeled 'A', 'B', and 'C', and three rows with the specified index and containing the values from the corresponding lists.

code snippet

import pandas as pd
import numpy as np

data = {'Name':['Swapnil','Rahul','Viraj','Pranav'],
'Age':[28,34,29,42]}
df = pd.DataFrame(data)
print(df)

df = pd.DataFrame(data,
index=['rank1','rank2','rank3','rank4'])
print(df)

df = pd.DataFrame(data,
index=['rank1','rank2','rank3','rank4'],
columns=['Name','Age'])
print(df)

@ParagDhawan
=============================================================================
Link for Tutorial Series

Jupyter Notebook Tutorial Series:-

Python Tutorial Series:-

Python Assignments and Objective Questions:-

Tech. Videos By Parag Dhawan;-

Object-Oriented Programming in Python:-

File Handling in Python:-

Exception Handling in Python:-

NumPy Tutorial Series:-

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

=============================================================================
Show your support by Subscribing to the channel:-
=============================================================================

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

============================================================
How to Record Your Screen and make a tutorial video or demo video: -
============================================================
Рекомендации по теме
visit shbcf.ru