简体   繁体   English

键在字典python中显示为nan

[英]Key is shown as nan in dictionary python

I am trying to convert a dataframe with 2 columns as dictionary with first column being key and second its value all in one dictionary.我正在尝试将具有 2 列的数据框转换为字典,其中第一列是键,第二列是其值都在一个字典中。

Data sample:数据样本:

id           summary
135791059    blha blah blah
135791051    blah something blah

And here is the code I have tried这是我试过的代码

map_of_values = pd.Series(f_dataframe.summary.values,index=f_dataframe.id).to_dict()

print(map_of_values)

The output is:输出是:

{'id': {'blah blah blah': nan, 'blah something blah}

I want it to be :我希望它是:

 {135791059:'blah blah blah blah',135791051:'blah something blah'}

What wrong am I doing?我做错了什么? And also I want to iterate over each key value pair builing a string from it.而且我想迭代每个键值对,从中构建一个字符串。 Is it the right way?这是正确的方法吗?

您可以简单地使用以下代码:

df.set_index('id').to_dict()['summary']

Tried your solution as below.尝试了您的解决方案,如下所示。

working fine.工作正常。 Unable to reproduce the error.无法重现错误。 Maybe you want to check into your created data-frame.也许您想检查您创建的数据框。

f_dataframe= {'id':[135791059,135791051],
        'summary':["blha blah blah", "blah something blah"]}
df = pd.DataFrame(f_dataframe)

map_of_values = df.set_index('id').to_dict()['summary']
print(map_of_values)
  • df.to_dict() method is used to convert a dataframe into a dictionary of series or list like data type depending on orient parameter. df.to_dict()方法用于根据orient参数将数据帧转换为系列或列表等数据类型的字典。
  • orient - String value, ('dict', 'list', 'series', 'split', 'records', 'index') Defines which dtype to convert Columns(series into). orient - 字符串值, ('dict', 'list', 'series', 'split', 'records', 'index')定义将 Columns(series) 转换为哪个 dtype。

Ex.前任。

import pandas as pd

my_dict = {'id':[135791059,135791051],'summary':["blha blah blah", "blah something blah"]}
df = pd.DataFrame(my_dict)
print(df)
records = df.to_dict('records')
data = {i['id'] :i['summary'] for i in records}
print(data)

O/P:开/关:

          id              summary
0  135791059       blha blah blah
1  135791051  blah something blah

{135791059: 'blha blah blah', 135791051: 'blah something blah'}

This problem mostly happens if you already have used the values as a column.如果您已经将值用作列,则通常会发生此问题。 IF you have an existing column and want to attribute some 'key's to the values and make a new column, it will appear as NaN .如果您有一个现有列并希望将某些“键”归因于值并创建一个新列,它将显示为NaN You need to use the existing column as the 'key' and the desired new column as 'values' (by just switching the position of key and value in your dictionary).您需要将现有列用作“键”,将所需的新列用作“值”(只需在字典中切换键和值的位置)。

For example: your pre-existing column is summary and your current dict is:例如:您预先存在的列是摘要,而您当前的字典是:

d = {135791059: 'blah blah blah', 135791051: 'blah something blah'}

However, You need to correct it like:但是,您需要更正它,例如:

d = {'blah blah blah': 135791059: , 'blah something blah': 135791051}

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

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