Lock-Aze
Lock-Aze

Reputation: 28

Error when using list comprehension to add dict to dataframe object

Having trouble adding dict objects to dataframe when using dict comprehension.

I have some code that I keep getting TypeError: 'float' object is not subscriptable on, if I run the same code with print it works

The dataframe I have looks like this:

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

I Would like it to look like this:

organisasjonsnummer kode          beskrivelse
981260546           2100        'Private aksje'
913062159           2100        'Private aksje'
975931366           2100        'Private aksje'

So I tried to append to the dataframe like this, but I can't get it to work...

Dataframe_test['kode'] = [x.get('kode') for x in Dataframe_test['institusjonellSektorkode']]
# This doesn't work
sample = [x['kode'] for x in SAMPLE_TEST['institusjonellSektorkode']]

# this works
sample = [print(x['kode']) for x in SAMPLE_TEST['institusjonellSektorkode']]

Upvotes: 0

Views: 102

Answers (2)

Lock-Aze
Lock-Aze

Reputation: 28

Oh my.... I found out what went wrong.... I had an error in my dataset. This is the way I corrected it.... lesson learned... Check/wash dataset better next time..

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']]

Upvotes: 0

jezrael
jezrael

Reputation: 862731

I think data are not dicts, but strings in column institusjonellSektorkode, so need converting them before by ast.literal_eval in list comprehension, create new DataFrame and join to original. Function pop is for extracting column:

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

Upvotes: 1

Related Questions