以下是数据帧的子集。 我想创建4个列表:
list 1: list of all WD1 as follows:
[flu-like symptoms, dizziness, major mood swings, lots of anxiety, tiredness, Dizziness, headaches, neck pain, headache, nausea]
list 2: comment_id: [1, 1, 1, 1, 1, 14, 14, 14, 17, 17]
list 3 drug_id [lex.1, lex.1, lex.1, lex.1, lex.1, lex14, lex14, lex14, lex18, lex18]
如您所见,如果WD列中的值不是NAN,我将捕获该值的注释id和药品id
我知道我可以使用以下代码迭代行以捕获每个WD:
for index, row in df.iterrows()
但我不知道怎么说if it is not NAN
而且,当我将捕获的值添加到已经定义的列表中时,该列表不会返回列表。列表中的数据是字符串格式
您可以通过
cumcount()
创建一个rowid
,该rowid
对应于comment_id
和drug_id
的每个组合中的列索引,然后用两个id列作为索引将其取消堆叠:数据设置:
更新:
看起来您想要相反的结果,给定数据帧
df1
,您可以首先将其转换为长格式,然后每个列都是您需要的,您可以使用tolist()
来转换它们:相关问题 更多 >
编程相关推荐