Home > Mobile >  python for loop in column condition
python for loop in column condition

Time:03-30

I just want to make a dictionary with the value of cate4 column in dataframe but seems all value in the dictionary are same

the cate_content table is like below

cate4 content
A sentence1
B sentence2
cate_dict = {}
noun_lists = []
for cate in cate_list:
  for content in cate_content[cate_content['cate4']==cate].content:
    noun_list = nlpy.nouns(content)
    for noun in noun_list:
      noun_lists.append(noun)
  cate_dict[cate] = noun_lists

CodePudding user response:

For your specific case, try this:

{row.cate4: nlpy.nouns(row.content) for _, row in cate_content.iterrows()}

CodePudding user response:

Use to_dict(). Just make sure you tokenize the whole column before and that's it (or tokenize later, but in any case, that's not the problem proposed here).

df = pd.DataFrame(data={"a":[1,2,3,4,5],"b":[2,3,4,5,6]})
records = df.to_dict(orient='records')
[{i["a"]: i["b"]} for i in records]
>> [{1: 2}, {2: 3}, {3: 4}, {4: 5}, {5: 6}]
  • Related