1

I have a dataframe containing dictionaries in their cells. First I filter the columns where there are None values, in order to iterate and explore the ones with dictionaries. T iterate over the columns with dictionaries and my goal is to sum the values of those dictionaries and add them to a new column pero column explored.

I keep getting the error:

TypeError: 'NoneType' object is not iterable

The dataframe columns are something like this:

[In] `df['column_A'].iloc[1]`
[Out] 
{'16875259': 0.0,
 '16909775': 9.0,
 '16909808': 214.0,
 '16909816': 3696.0,
 '16910032': 725.0,
 '16910067': 27.0,
 '16910182': 11.0,
 '16910237': 4.0,
 '16910262': 3191.0,
 '16910358': 52.0,
 '16910411': 73.0,
 '16910416': 111.0,
 '16910477': 1869.0,
 '16910598': 2425

The code I managed to build is somthing like this:

for column in my_df:
    if(my_df[column].values[1]!='None' or my_dfdf1[column].values[1] !=None):
        for column in df1:
            for item in df1[column].values:
                for i in item:
                    #print(i)
                    # Here I need to sum the values

I expect the sum of the values but I keep getting the error:

TypeError: 'NoneType' object is not iterable

Sociopath
  • 13,068
  • 19
  • 47
  • 75
tomruarol
  • 69
  • 7
  • 1
    [pandas example style for asking](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – ZF007 Apr 26 '19 at 09:25

2 Answers2

3

Use if-else statement for sum if values are dictionaries and DataFrame.applymap for elementwise processing:

df = pd.DataFrame({'a':[{'16875259': 0.0, '16909775': 9.0, '16909808': 214.0},
                        {'16875259': 0.0, '16909775': 10.0}, 
                        np.nan],
                    'b':[{'16875259': 3.0, '16909775': 9.0, '16909808': 214.0},
                         np.nan,
                        {'16875259': 4.0, '16909775': 10.0}, 
                        ]})
print (df)
0  {'16875259': 0.0, '16909775': 9.0, '16909808':...   
1                {'16875259': 0.0, '16909775': 10.0}   
2                                                NaN   

                                                   b  
0  {'16875259': 3.0, '16909775': 9.0, '16909808':...  
1                                                NaN  
2                {'16875259': 4.0, '16909775': 10.0}  

df = df.applymap(lambda x: sum(x.values()) if isinstance(x, dict) else np.nan)
print (df)
       a      b
0  223.0  226.0
1   10.0    NaN
2    NaN   14.0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

can you try the following:

df['column_A'].apply(lambda x: sum(x.values()))
Jeril
  • 7,858
  • 3
  • 52
  • 69