0

I'm trying to take x number of columns from an existing df and convert them into a dictionary.

My questions are:

  1. The method shown below is considered a good practice? I think it's repetitive and I'm sure it can be a more elegant code.

  2. Should I convert from df to dictionary if my idea is to build a plot? Or it's an unnecessary step?

I've tried the code below:

familiarity_dic = familiarity[{'Question':'Question','SCORE':'SCORE'}]

familiarity_dic

Expected result is correct but I want to know if it's the best practice for Pandas.

Question SCORE 36 Invesco 100 35 Schroders 96 34 Fidelity 96 31 M&G 95 0 BlackRock 95

2 Answers2

0

I think easier to plot from pandas than a dict. Try using df.plot(). You can subset your df as required to only plot the information you're interested it.

0

You can convert your df to a dict using pandas built in method df.to_dict()... you can find more about it here. As far as whether or not you need to convert the df who really knows? You'll find people who will tell you it's easier to plot from df while others may advocate a dict... also you have to take into account what visualization (matplotlib, Tableau, etc.) you're using. You may want to try both to find out which works best for you...

JC23
  • 1,248
  • 4
  • 18
  • 28