Issue
I am working on a situation where I need to convert a dataframe into dictionary of lists. Example dataframe is below :
I want to convert above dataframe into dictionary of lists like below :
dict = {"abc":[sentence 1, sentence 2], "def":[sentence 3], "ghi":[sentence 4, sentence 5]}
Here is my solution :
dict = {}
for idx, row in test_df.iterrows():
if not row["label"] in dict:
dict[row["label"]] = []
else:
continue
for key in dict:
dict[key] = list()
for idx, row in test_df.iterrows():
if key == row["label"]:
dict[key].append(row["sentence"])
else:
continue
print(dict)
My solution works but it looks like a lot of code and there should be a easy way out. Any suggestions?
Solution
data = pd.DataFrame([
{"sentence": "sentence1", "label":"abc"},
{"sentence": "sentence2", "label":"abc"},
{"sentence": "sentence3", "label":"def"},
{"sentence": "sentence4", "label":"ghi"},
{"sentence": "sentence5", "label":"ghi"},
])
data
sentence label
0 sentence1 abc
1 sentence2 abc
2 sentence3 def
3 sentence4 ghi
4 sentence5 ghi
data.groupby("label")["sentence"].apply(list).to_dict()
{'abc': ['sentence1', 'sentence2'],
'def': ['sentence3'],
'ghi': ['sentence4', 'sentence5']}
Answered By - galaxyan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.