This is my dict with emotions and their probabilities:
{'angry': 0.08,
'disgust': 0.0,
'fear': 0.16,
'happy': 0.05,
'sad': 0.02,
'surprise': 0.68,
'neutral': 0.02}
I want the emotions e.g, 'angry' as header, and the probabilities as rows. Does someone know how to make a df of it?
CodePudding user response:
Also, as correctly posted in other answers, if you want to adjust your dictionary into the correct format:
data = {k: [v] for k, v in data.items()}
Pandas DataFrame documentation
CodePudding user response:
d = {
'angry': 0.08,
'disgust': 0.0,
'fear': 0.16,
'happy': 0.05,
'sad': 0.02,
'surprise': 0.68,
'neutral': 0.02
}
pd.DataFrame(data=[d.values()], columns=d.keys())
Result:
angry disgust fear happy sad surprise neutral
0 0.08 0.0 0.16 0.05 0.02 0.68 0.02
CodePudding user response:
First you need to pass for each key
in your dict
a list of values:
{'angry': [0.08],'disgust': [0.0],'fear': [0.16],'happy': [0.05],'sad': [0.02],'surprise': [0.68],'neutral': [0.02]}
Then you can simply use pandas
like this:
import pandas as pd
dict = {'angry': [0.08],'disgust': [0.0],'fear': [0.16],'happy': [0.05],'sad': [0.02],'surprise': [0.68],'neutral': [0.02]}
df = pd.DataFrame(dict)
print(df)
>> angry disgust fear happy sad surprise neutral
0 0.08 0.0 0.16 0.05 0.0 0.68 0.02
CodePudding user response:
You can use a Series and convert to DataFrame:
pd.Series(d).to_frame().T
or use pandas.DataFrame.from_dict
:
pd.DataFrame.from_dict(d, orient='index').T
output:
angry disgust fear happy sad surprise neutral
0 0.08 0.0 0.16 0.05 0.02 0.68 0.02
input:
d = {'angry': 0.08,
'disgust': 0.0,
'fear': 0.16,
'happy': 0.05,
'sad': 0.02,
'surprise': 0.68,
'neutral': 0.02}