ferrelwill
ferrelwill

Reputation: 821

How to turn dictionary into a dataframe with only a subset of columns using pandas?

I am trying to convert the following dictionary format into dataframe with no success. How can I do that using instance_score and feature_score in input as columns in a dataframe?

input

{'data': {'f_score': None,
  'instance_score': array([ 0.99999967, -0.37898901, -0.38409877, ...,  0.0470927 ,
         -0.07509494,  0.32273143]),
  'feature_score': array([1, 0, 0, ..., 0, 0, 0])},
 'meta': {'data_type': 'time-series',
  'detr_type': 'line',
  'name': 'ad'}}

code

pd.DataFrame.from_dict(input)

Upvotes: 4

Views: 887

Answers (1)

cs95
cs95

Reputation: 402814

You can call pd.DataFrame, specifying a subset of the columns you want to keep:

pd.DataFrame(d['data'], columns=['instance_score', 'feature_score'])

   instance_score  feature_score
0        1.000000              1
1       -0.378989              0
2       -0.384099              0
3        0.047093              0
4       -0.075095              0
5        0.322731              0

Upvotes: 4

Related Questions