-->

Monday, July 20, 2015

Turn a {key, value} Python Dictionary into a Pandas DataFrame

Quick solution to a problem I had today. I had a dictionary of {key, values} that I wanted into a dataframe. My solution:

import pandas as pd
pd.DataFrame([[key,value] for key,value in python_dict.iteritems()],columns=["key_col","val_col"])

6 comments:

  1. why do you want to do that when it can be done directly as :
    l = [{k1:v11, k2:v21},
    {k1:v21, k2:v22}, .....]

    pd.DataFreame(l)

    ReplyDelete
    Replies
    1. That only works if you already have a list. My usecase was when I only had a single dictionary full of keys and values. You have to make it a list first.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Would it be easier to use pd.Series(python_dict).to_frame()

    ReplyDelete
  4. How concerned are they about protecting the Master Key? Beware locksmiths that are flippant about the Master Key's security. Remember, every one of their employees that works on the system can potentially recreate a key.
    duplicate key maker near me

    ReplyDelete
  5. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
    selenium Training in Marathahalli

    ReplyDelete