Issue
I have an output which contains scores and the word with topic number, how can i save it in dataframe with 3 different columns? a: topic name, b : words, c : scores?
Solution
For your specific dict you could use something like this:
import pandas as pd
from collections import defaultdict
d = {
-1: [('gun', 0.03), ('shut', 0.2)],
0: [('http', 0.08), ('wireless', 0.1)],
}
data = defaultdict(list)
for key in d:
data["topic_name"] += [key] * len(d[key])
data["words"] += [i[0] for i in d[key]]
data["scores"] += [i[1] for i in d[key]]
pd.DataFrame(data)
-------------------------------------------------------
topic_name words scores
0 -1 gun 0.03
1 -1 shut 0.20
2 0 http 0.08
3 0 wireless 0.10
-------------------------------------------------------
For a nested dict, you could use something like this:
import pandas as pd
from collections import defaultdict
d = {
-1: {
'gun':0.1,
'people': 0.2,
'chut': 0.3
},
0: {
'http': 0.03,
'tco': 0.06,
'wireless': 0.8
}
}
data = defaultdict(list)
for key in d:
data["topic_name"] += [key] * len(d[key])
data["words"] += list(d[key].keys())
data["scores"] += list(d[key].values())
pd.DataFrame(data)
This results to the following data frame:
topic_name words scores
0 -1 gun 0.10
1 -1 people 0.20
2 -1 chut 0.30
3 0 http 0.03
4 0 tco 0.06
5 0 wireless 0.80
Answered By - ko3
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.