0

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,

  },
];
LeCoda
  • 538
  • 7
  • 36
  • 79
  • 3
    Can you give a minimal example, of what the relevant part of your pd.DataFrame looks like as well as the expected output? – Christian Steinmeyer May 10 '22 at 09:13
  • [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – BeRT2me May 16 '22 at 07:46

1 Answers1

2

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.

Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74