Home > Blockchain >  How extract variables from series
How extract variables from series

Time:04-24

I have variable of type pandas.core.series.Series that consist several variables and their values as below example and I want to extract each variable to new series and its value. How i can extract them in easiest way?

{'anger': 0.014778325123152709, 'anticipation': 0.0049261083743842365, 'disgust': 0.024630541871921183, 'fear': 0.0049261083743842365, 'joy': 0.0, 'negative': 0.054187192118226604, 'positive': 0.019704433497536946, 'sadness': 0.0049261083743842365, 'surprise': 0.0049261083743842365, 'trust': 0.014778325123152709}

CodePudding user response:

you want to extract keys on separate series and values on separate series?

import pandas as pd

m={'anger': 0.014778325123152709, 'anticipation': 0.0049261083743842365, 
'disgust': 0.024630541871921183, 
'fear': 0.0049261083743842365, 'joy': 0.0, 'negative': 
0.054187192118226604, 'positive': 0.019704433497536946, 
'sadness': 0.0049261083743842365, 'surprise': 0.0049261083743842365, 
'trust': 0.014778325123152709}
a = pd.Series(m)

g = pd.Series(a.index)
print(g)

h = pd.Series(a.values)
print(h) 

CodePudding user response:

Makes each variable into its own series. Adding them to a list. I can't be sure this is what you want based on your question.

m = {'anger': 0.014778325123152709, 'anticipation': 0.0049261083743842365,
     'disgust': 0.024630541871921183, 'fear': 0.0049261083743842365, 
     'joy': 0.0, 'negative': 0.054187192118226604, 
     'positive': 0.019704433497536946, 'sadness': 0.0049261083743842365,
     'surprise': 0.0049261083743842365, 'trust': 0.014778325123152709}
s = [pd.Series(x) for x in m.items()]
for item in s:
    print(item)
    print()

Output:

0       anger
1    0.014778
dtype: object

0    anticipation
1        0.004926
dtype: object

0     disgust
1    0.024631
dtype: object

0        fear
1    0.004926
dtype: object

0    joy
1    0.0
dtype: object

0    negative
1    0.054187
dtype: object

0    positive
1    0.019704
dtype: object

0     sadness
1    0.004926
dtype: object

0    surprise
1    0.004926
dtype: object

0       trust
1    0.014778
dtype: object
  • Related