-1

I have generated a dictionary which I would like to convert to a pandas dataframe.

My intention is for the keys to be the column headers and the values to fill the rows. The values are saved in lists, whose order needs to be preserved.

This is different from a list of dictionaries.

Here is an example of the data structure:

{'training_samples': ['200', '200', '100', '100'], 'shape': ['gaussian', 'gaussian','gaussian', 'gaussian'], 'bandwidth': ['2.0', '1.0', '2.0', '1.0'], 'entropy': [3.0220786946957032, 3.551872762572949, 3.0158071153302135, 3.5436555507182104]}

Thanks in advance!

Sammy
  • 1
  • 1

1 Answers1

0
import pandas as pd

dictionary = {'training_samples': ['200', '200', '100', '100'], 'shape': ['gaussian', 
              'gaussian','gaussian', 'gaussian'], 'bandwidth': ['2.0', '1.0', '2.0', 
              '1.0'], 'entropy': [3.0220786946957032, 3.551872762572949, 
               3.0158071153302135, 3.5436555507182104]}

print(dictionary)

dataframe = pd.DataFrame(dictionary)

print(dataframe)

Hi Sammy.

  1. I have imported pandas as pd.
  2. I have stored the required information in a variable called 'dictionary' which is of a dictionary data structure.
  3. We utilize pandas' DataFrame method in order to conduct the transformation, from a dictionary to a dataframe.
  4. By default, pandas will preserve the order of values, during the dataframe compilation stage. :)
  5. When you convert a dictionary into a dataframe, the keys become column headers and the values will occupy the rows, creating individual observations.
Destroyer-byte
  • 121
  • 1
  • 4