'returning dataframe as dictionary, can't get right format

I'm using Python to try and return a dataset after transforming it a bit.

Currently, it works like this, with the data it's returning.

[
    {
        "index": "angry",
        ....
    },
    .....
    {
        "index": "dose",
        ......
        "medication": 0.822,
        "dose": 1.0
    }
]

This being returned by this function, using to_dict the pandas method,

return SubstanceTimingBiometric(Expressions,substance,scheduled_medication,emotion).corr().round(3).reset_index().to_dict("records")

I really just want to return one json / dict object, being the index at medication. How would I do this simply?

the example input, and output I suppose,but with sentiment instead of expressions (so not using angry, sad, etc).

enter image description here

it's just in a basic dataframe that looks like this above before re-indexing etc

Did this which fixed it,

expression_dfc[expression_dfc.index=="medication"]

now I want to transform it to be more like a separate format.

i.e.,

const correlation_sentiment= [
  {
      "index": "dose",
      "angry": 0.004,
      "disgusted": 0.002,
      "fearful": 0.008,
      "happy": -0.032,
      "neutral": 0.004,
      "sad": 0.042,
      "surprised": -0.034,
      "medication": 0.822,
      "dose": 1.0
  }
]

const data = [
  {
    emotion: 'angry',
    medication: -.10,
  },
  {
    emotion: 'disgusted',
    medication: .05,

  },
  {
    emotion: 'fearful',
    medication: .35,

  },
  {
    emotion: 'happy',
    medication: .10,

  },
  {
    emotion: 'neutral',
    medication: .220,

  },
  {
    emotion: 'sad',
    medication: .120,

  },
  {
    emotion: 'surprised',
    medication: -.120,

  },
];


Solution 1:[1]

It's hard to deduce what your actual question is, what you expect to do. My guess would be something like:

df = SubstanceTimingBiometric(Expressions, substance, scheduled_medication, emotion)
correlations = df.corr()
dfc = correlations.round(3).reset_index()
return dfc['medication'].drop('medication').to_dict()

Better, if you only need the correlation with medication, why do you calculate them all? Try .corrwith.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1