Create Pandas Dataframe Dictionary With Tuple As Key
Last Updated :
27 Jan, 2024
In Python, Pandas is a powerful data manipulation library, and a dataframe is a data structure. In this article, we will explore how to create a Pandas Dataframe Dictionary with Tuple as a Key.
What is a data frame?
Dataframes are a fundamental data structure. A data frame is a two-dimensional data structure or tabular data structure with labelled axis rows and columns. It is mainly used for storing and manipulating tabular data. In simple words, it is similar to an Excel spreadsheet or SQL table with rows and columns. It is very useful for analyzing the data.
What do you mean by tuple as key?
In Python, Tuple is an immutable data structure and an ordered collection of elements. Tuple as key means we use tuples as keys in the dictionary.
Mainly, we use a tuple as a key, where a composite key uniquely identifies values.
Example
Python
d={(7,18,14):"Cricket"}
print(d)
Output:
{(7, 18, 14): 'Cricket'}
In this example (7,8,14) is a tuple stored as a key in the dictionary.
Create Pandas Dataframe Dictionary With Tuple As Key
Let's understand how to create a Pandas Dataframe Dictionary With Tuple as a key for example. I have created two sample dataframes first is to store student basic information and the second is to store grade information we create a new dictionary dataframe where tuples serve as keys to uniquely identify each dataframe.
Step 1: Import Pandas
First, we need to import pandas into our program.
Python
If pandas are not installed in your system first install the pandas by using "pip install pandas"
Step 2: Create Sample Data
Here, the dictionary data
has tuples as keys and dictionaries as values. Each key-value pair in the dictionary represents information about an individual.
Python3
data = {
('Alice', 25): {'City': 'New York', 'Occupation': 'Engineer'},
('Bob', 30): {'City': 'San Francisco', 'Occupation': 'Data Scientist'},
('Charlie', 22): {'City': 'Los Angeles', 'Occupation': 'Student'}
}
Step 3: Creating a DataFrame from the dictionary
Python3
df = pd.DataFrame.from_dict(data, orient='index')
# Displaying the DataFrame
print(df)
Output:
City Occupation
Alice 25 New York Engineer
Bob 30 San Francisco Data Scientist
Charlie 22 Los Angeles Student
Each row in the DataFrame corresponds to an individual (identified by the index, which is a tuple), and the columns represent the attributes 'City' and 'Occupation' with their respective values.
Similar Reads
How to convert Dictionary to Pandas Dataframe? Converting a dictionary into a Pandas DataFrame is simple and effective. You can easily convert a dictionary with key-value pairs into a tabular format for easy data analysis. Lets see how we can do it using various methods in Pandas.1. Using the Pandas ConstructorWe can convert a dictionary into Da
2 min read
Create pandas dataframe from lists using dictionary Pandas DataFrame is a 2-dimensional labeled data structure like any table with rows and columns. The size and values of the dataframe are mutable, i.e., can be modified. It is the most commonly used pandas object. Creating pandas data-frame from lists using dictionary can be achieved in multiple way
2 min read
Creating a Pandas dataframe using list of tuples A Pandas DataFrame is an important data structure used for organizing and analyzing data in Python. Converting a list of tuples into a DataFrame makes it easier to work with data. In this article we'll see ways to create a DataFrame from a list of tuples.1. Using pd.DataFrame()The simplest method to
2 min read
How To Convert Pandas Dataframe To Nested Dictionary In this article, we will learn how to convert Pandas DataFrame to Nested Dictionary. Convert Pandas Dataframe To Nested DictionaryConverting a Pandas DataFrame to a nested dictionary involves organizing the data in a hierarchical structure based on specific columns. In Python's Pandas library, we ca
2 min read
Create PySpark dataframe from dictionary In this article, we are going to discuss the creation of Pyspark dataframe from the dictionary. To do this spark.createDataFrame() method method is used. This method takes two argument data and columns. The data attribute will contain the dataframe and the columns attribute will contain the list of
2 min read
Dictionary with Tuple as Key in Python Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to
4 min read