繁体   English   中英

我有数据框。 我需要创建一个以行为键的字典,以“True”作为字典值的列

[英]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      

我需要创建这样的东西

字典= {3: [1,3,4,6,7,8], 4: [0,1,3,4,5,6,7]}

我希望我已经理解你的问题了:

out = dict(
    df.apply(lambda x: [int(i) for i, v in zip(x.index, x) if v], axis=1)
)
print(out)

印刷:

{3: [1, 3, 4, 6, 7, 8], 4: [1, 3, 4, 5, 6, 7]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM