I have a data frame named train_df
:
text | label |
---|---|
I'm loving Instagram | |
I'm loving Linkedin | |
I'm loving Facebook | |
I'm loving Twitter |
I am trying to put the text into a list and put the lists into a dictionary with the keys being the label e.g
my_dict = {'Instagram' : [I'm loving Instagram], 'Linkedin': [I'm loving Linkedin]...}
At the moment, the code I've written to get this done is:
my_dict = dict()
for i in train_df.label.unique():
my_dict[i] = " ".join(train_df[train_df["label"] == "@i"]["text"].tolist())
I get a dictionary as described above but the values are empty. I'm not sure what I'm doing wrong and any help is really appreciated. Thank you so much!!!
CodePudding user response:
i hope that helps you:
my_dict = dict()
for i in train_df.label.unique():
my_dict[i] = train_df.loc[train_df.label == i, 'text'].values.tolist()
CodePudding user response:
Group by 'label' (train_df.groupby('label')
), aggregate the texts of the groups into lists (...['text'].agg(list)
), and then convert the resulting DataFrame to a dictionary using the to_dict
method
my_dict = train_df.groupby('label')['text'].agg(list).to_dict()
CodePudding user response:
# Convert to lists
textList = df["text"].tolist()
labelList = def["label"].tolist()
# Create a dictionary
my_dict = {}
for i, label in enumerate(labelList):
my_dict[label] = textList[i]