2 回答
TA贡献1799条经验 获得超6个赞
我认为数据不是字典,而是列中的字符串institusjonellSektorkode,所以需要ast.literal_eval在列表理解之前转换它们,创建新的DataFrame并加入原始的。功能pop是提取列:
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
TA贡献1866条经验 获得超5个赞
哦,我的......我发现出了什么问题......我的数据集中有一个错误。这就是我纠正它的方式......吸取教训......下次更好地检查/清洗数据集......
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']]
添加回答
举报