简体   繁体   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      

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.

相关问题 字典有一个单独的字典,我想在 python 中的 dataframe 中转换它,以便该表包含具有子列的列 - A dictionary has a separate dictionary and i want to convert it in dataframe in python such that the table contains columns which has sub columns 我需要编写一个包含字符串和值的字典 - I need to write a dictionary in which it has both strings and values 字典到反转的数据帧。 - Dictionary to inversed dataframe. 如果每列每行有多个值,如何在熊猫数据框中的两列之间创建字典? - How can I create a dictionary between two columns within a pandas dataframe if each column has more than one value per row? 我在熊猫数据框中有两列。 一列具有相同的值(id),另一列中具有对应的值的票证数 - I have two columns in a pandas dataframe. One column has some same values (id) and corresponding values in another column has Number of tickets 我在 pandas dataframe 列中有字典作为值。 我想将键列和值作为列值 - I have dictionary as value in pandas dataframe columns. I want to make the keys columns and values as column value 如何从字典创建一个数据框,其中键和值都是列? - How to create a Dataframe from Dictionary where the key and values are all columns? dataframe 列中存在的字典中键的映射值 - Mapping Values of key in Dictionary present in dataframe columns 我有两个列表,我想从中创建一个字典。 但是我想为一个键存储多个值 - I have two list and I want to create a dictionary from it . But I want to store multiple values for one key DataFrame 按具有字典值的列分组 - DataFrame groupby a column which has dictionary values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM