How to create a dictionary of two pandas DataFrame columns
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Creating a dictionary from two DataFrame columns is a common way to turn tabular data into a lookup structure. The simplest solution is short, but correctness depends on how you want to handle duplicate keys, missing values, and column types. If you decide those rules up front, the conversion is straightforward and reliable.
Use zip for the Basic Case
When one column should become keys and another should become values, zip is the most direct approach.
Output:
This is clear and efficient for normal one-to-one mappings.
Use set_index(...).to_dict() for DataFrame Pipelines
Another idiomatic Pandas pattern is to make one column the index and convert the other to a dictionary.
This reads nicely when you are already performing index-oriented operations in a Pandas pipeline.
Duplicate Keys Need an Explicit Policy
Python dictionaries cannot keep multiple values under the same key unless you design them to. If duplicate keys exist, later rows overwrite earlier ones in the normal conversion.
Output:
If that is not the intended rule, handle duplicates explicitly.
Keep the first occurrence:
Keep all values per key:
The important part is not the exact method. It is making the duplicate-key rule explicit rather than accepting accidental overwrite behavior.
Clean Missing Data Before Converting
Missing keys or values can produce awkward lookup results. If the mapping should only contain complete entries, drop incomplete rows first.
If missing values are meaningful in your application, keep them intentionally and document the behavior.
Wrap the Conversion in a Helper
If the pattern appears in several places, define a helper so duplicate and missing-value rules stay consistent.
That is easier to test than repeating slightly different conversion code in notebooks, scripts, and services.
Consider Composite Keys When One Column Is Not Enough
Sometimes the real key is a combination of two columns. A dictionary can still handle that by using tuples as keys.
This is often clearer than pretending one column alone is unique when it is not.
Common Pitfalls
The most common pitfall is forgetting that duplicate keys overwrite earlier values in a normal dictionary conversion.
Another issue is converting without checking for missing values, then debugging odd lookup behavior later.
People also sometimes rebuild the same mapping repeatedly inside loops when it could be created once and reused.
Finally, if the mapping crosses a system boundary such as JSON output, remember that some key types are more portable than others.
Summary
- Use
dict(zip(df[key], df[value]))for the simplest two-column mapping. - '
set_index(...).to_dict()is another idiomatic Pandas approach.' - Decide how duplicate keys should behave before converting.
- Drop or handle missing values intentionally.
- Use tuple keys when the real lookup key spans multiple columns.
Related reading
- How to create a neural network for regression?
- How to create a Tensorflow Tensorboard Empty Graph
- How to Create Dataframe from AWS Athena using Boto3 get_query_results method
- How to create dataset similar to cifar-10
- How to create a drop-down list?
- How to create a fixed-size array of objects
- How to create a file name with the current date time in Python?
- How to create a GUID/UUID in Python

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.