Introduction Many times, you will want to search through a dataframe so many times that both writing the search queries using dataframe rules and programmatically the search operation becomes redundant. In such cases, you will want to create a dictionary from a pandas dataframe. Now, if you wonder how, then I have the solution. Read on to explore more. to_dict() method: Possible is that you want to turn the rows into values while keeping one specific column as index. In such a case, you have to use the method as follows df.set_index('id').to_dict() zip and dict method: You can use a simpler method and can get done in one line, if your goal is to turn one specific column into keys and one specific column into values, then you can use the zip and dict method. The details follows as below: data_dict = dict(zip(data[key_column],data[value_column])) Duplicate document problem: This is a case when you have multiple values for each key. In that case, it will become a bit...
I write about machine learning models, python programming, web scraping, statistical tests and other coding or data science related things I find interesting. Read, learn and grow with me!