[英]I have dataframe. I need to create a dictionary with row as the key and columns which has 'True' as the values of the dictionary
0 1 2 3 4 5 6 7 8 9
3 False True False True True False True True True False
4 False True False True True True True True False False
I need to create something like this我需要创建这样的东西
dict= {3: [1,3,4,6,7,8], 4: [0,1,3,4,5,6,7]}字典= {3: [1,3,4,6,7,8], 4: [0,1,3,4,5,6,7]}
I hope I've understood your question right:我希望我已经理解你的问题了:
out = dict(
df.apply(lambda x: [int(i) for i, v in zip(x.index, x) if v], axis=1)
)
print(out)
Prints:印刷:
{3: [1, 3, 4, 6, 7, 8], 4: [1, 3, 4, 5, 6, 7]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.