Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将dataframe转换为字典忽略某些值_Python_Pandas_Dataframe_Dictionary - Fatal编程技术网

Python 将dataframe转换为字典忽略某些值

Python 将dataframe转换为字典忽略某些值,python,pandas,dataframe,dictionary,Python,Pandas,Dataframe,Dictionary,我需要帮助将dataframe转换为字典,如下所示,其中id是主键,内部字典的值应大于0: 给定数据帧: id score1 score2 score3 score4 score5 1 0.0000 0.1087 0.0000 0.0786 1 2 0.0532 0.3083 0.2864 0.4464 1 3 0.0000 0.0840 0.8090 0.2331 1 预期解决方案: [1:{'score2': 0.1

我需要帮助将dataframe转换为字典,如下所示,其中id是主键,内部字典的值应大于0:

给定数据帧:

id  score1  score2 score3  score4     score5
1  0.0000  0.1087  0.0000  0.0786       1
2  0.0532  0.3083  0.2864  0.4464       1
3  0.0000  0.0840  0.8090  0.2331       1
预期解决方案:

[1:{'score2': 0.10865899999999999,
  'score4': 0.078597,
  'score5': 1.0},
 2:{'score1': 0.053238000000000001,
  'score2': 0.308253,
  'score3': 0.28635300000000002,
  'score4': 0.44643299999999997,
  'score5': 1.0},
 3:{'score2': 0.083978999999999998,
  'score3': 0.80898300000000001,
  'score4': 0.23305200000000001,
  'score5': 1.0}]
[{'id': 1.0,
  'score1': 0.0,
  'score2': 0.10865899999999999,
  'score3': 0.0,
  'score4': 0.078597,
  'score5': 1.0},
 {'id': 2.0,
  'score1': 0.053238000000000001,
  'score2': 0.308253,
  'score3': 0.28635300000000002,
  'score4': 0.44643299999999997,
  'score5': 1.0},
 {'id': 3.0,
  'score1': 0.0,
  'score2': 0.083978999999999998,
  'score3': 0.80898300000000001,
  'score4': 0.23305200000000001,
  'score5': 1.0}]
我的解决方案: 我使用
df.to_dict(orient='records')
给出以下解决方案:

[1:{'score2': 0.10865899999999999,
  'score4': 0.078597,
  'score5': 1.0},
 2:{'score1': 0.053238000000000001,
  'score2': 0.308253,
  'score3': 0.28635300000000002,
  'score4': 0.44643299999999997,
  'score5': 1.0},
 3:{'score2': 0.083978999999999998,
  'score3': 0.80898300000000001,
  'score4': 0.23305200000000001,
  'score5': 1.0}]
[{'id': 1.0,
  'score1': 0.0,
  'score2': 0.10865899999999999,
  'score3': 0.0,
  'score4': 0.078597,
  'score5': 1.0},
 {'id': 2.0,
  'score1': 0.053238000000000001,
  'score2': 0.308253,
  'score3': 0.28635300000000002,
  'score4': 0.44643299999999997,
  'score5': 1.0},
 {'id': 3.0,
  'score1': 0.0,
  'score2': 0.083978999999999998,
  'score3': 0.80898300000000001,
  'score4': 0.23305200000000001,
  'score5': 1.0}]

我假设您的预期输出是一个dict of dict,您可以使用

df.set_index('id').agg(lambda x: x[x != 0].to_dict(), axis=1).to_dict()


详细信息

将ID设置为索引,使其成为输出dict中的键:

df.set_index('id')

    score1  score2  score3  score4  score5
id                                        
1   0.0000  0.1087  0.0000  0.0786       1
2   0.0532  0.3083  0.2864  0.4464       1
3   0.0000  0.0840  0.8090  0.2331       1
接下来,将每行转换为字典,删除值等于0的列:

_.agg(lambda x: x[x != 0].to_dict(), axis=1)

id
1    {'score2': 0.1087, 'score4': 0.0786, 'score5':...
2    {'score1': 0.0532, 'score2': 0.3083, 'score3':...
3    {'score2': 0.084, 'score3': 0.809, 'score4': 0...
dtype: object
最后一步是将其转换为dict of dict:

_.to_dict()

{1: {'score2': 0.1087, 'score4': 0.0786, 'score5': 1.0},
 2: {'score1': 0.0532,
  'score2': 0.3083,
  'score3': 0.2864,
  'score4': 0.4464,
  'score5': 1.0},
 3: {'score2': 0.084, 'score3': 0.809, 'score4': 0.2331, 'score5': 1.0}}

这太棒了!是否也可以根据id值进行过滤。例如,如果我想显示ID在[1,2]中的位置。(不是3个)?怎么做?@user15051990您可以从
df=df[df['id'].isin([1,2])]
开始,然后像往常一样在我的答案中使用代码。有关更多信息,请参阅。