Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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 使用列表理解将dict添加到数据帧对象时出错_Python_Json_Pandas_List Comprehension - Fatal编程技术网

Python 使用列表理解将dict添加到数据帧对象时出错

Python 使用列表理解将dict添加到数据帧对象时出错,python,json,pandas,list-comprehension,Python,Json,Pandas,List Comprehension,在使用dict理解时,无法将dict对象添加到数据帧 我有一些代码,我一直得到TypeError:“float”对象是不可下标的,如果我运行与print相同的代码,它就会工作 我拥有的数据帧如下所示: organisasjonsnummer institusjonellSektorkode 981260546 {'kode': '2100', 'beskrivelse': 'Private aksje'} 913062159 {'kode': '2100'

在使用dict理解时,无法将dict对象添加到数据帧

我有一些代码,我一直得到TypeError:“float”对象是不可下标的,如果我运行与print相同的代码,它就会工作

我拥有的数据帧如下所示:

organisasjonsnummer institusjonellSektorkode
981260546           {'kode': '2100', 'beskrivelse': 'Private aksje'}
913062159           {'kode': '2100', 'beskrivelse': 'Private aksje'}
975931366           {'kode': '2100', 'beskrivelse': 'Private aksje'}
organisasjonsnummer kode          beskrivelse
981260546           2100        'Private aksje'
913062159           2100        'Private aksje'
975931366           2100        'Private aksje'

我希望它看起来像这样:

organisasjonsnummer institusjonellSektorkode
981260546           {'kode': '2100', 'beskrivelse': 'Private aksje'}
913062159           {'kode': '2100', 'beskrivelse': 'Private aksje'}
975931366           {'kode': '2100', 'beskrivelse': 'Private aksje'}
organisasjonsnummer kode          beskrivelse
981260546           2100        'Private aksje'
913062159           2100        'Private aksje'
975931366           2100        'Private aksje'

所以我尝试像这样附加到数据帧,但我无法让它工作

Dataframe_test['kode'] = [x.get('kode') for x in Dataframe_test['institusjonellSektorkode']]

我认为数据不是dicts,而是列
institutusjonellsektorkode
中的字符串,因此需要先通过列表理解中的
ast.literal\u eval
转换它们,创建新的
DataFrame
并连接到原始数据。函数用于提取列:

import ast

df1 = pd.DataFrame([ast.literal_eval(x) for x in df.pop('institusjonellSektorkode')])
print (df1)
     beskrivelse  kode
0  Private aksje  2100
1  Private aksje  2100
2  Private aksje  2100

df = df.join(df1)
print (df)
   organisasjonsnummer    beskrivelse  kode
0            981260546  Private aksje  2100
1            913062159  Private aksje  2100
2            975931366  Private aksje  2100

哦,天哪。。。。我发现哪里出了问题。。。。我的数据集中有一个错误。这就是我纠正它的方式。。。。吸取的教训。。。下次请检查/清洗数据集

import numpy as np

# Simple function to that returns a NaN if it is not fed a dict as an input.
def get_value(dict, string_to_get):
    '''
    takes input of dict, and tries to return the value of the string, if it fails
    it will return null value
    '''
    try:
        get_string = dict.get(string_to_get)
        return get_string
    except:
        return np.nan

Dataframe_test['kode'] = [get_value(x,'kode') for x in Dataframe_test['institusjonellSektorkode']]

不是舒尔,如果我误解了你所做的,但当我尝试时,我得到:“`ValueError:格式错误的节点或字符串:{'kode':'2500','beskrivelse':'Private ProduceNtorienter organisasjoner uten profittformål'”```