what is this error and how to get rid of it. TypeError: argument of type 'NoneType' is not iterable

877 Views Asked by At

I have a dataframe as below.. I want to highlight the cells if the value contains delimiter "--" I tried below code. but it throws me error :

a b c d
1--2 3 4 None
2 34--32 12 None
23 123--12 123--11 None
12 11--111 11--333 None
def color_red(val):
  color= 'red' if '|' in val else 'black'
  return 'color: %s' % color
s = df.style.applymap(color_red)
st = s.render()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python39\lib\site-packages\pandas\core\frame.py", line 7842, in infer
    return lib.map_infer(x.astype(object)._values, func, ignore_na=ignore_na)
  File "pandas\_libs\lib.pyx", line 2467, in pandas._libs.lib.map_infer
  File "<stdin>", line 2, in color_red
TypeError: argument of type 'NoneType' is not iterable
2

There are 2 best solutions below

0
jezrael On

You can try replace None to empty string before apply styles:

 s = df.fillna('').style.applymap(color_red)

If need in ouput NaNs is possible convert them to strings if check in statement:

df = pd.DataFrame({'a':[None, 'aa|ss'],
                   'b':['c', None]})


def color_red(val):
  color= 'red' if '|' in str(val) else 'black'
  return 'color: %s' % color
s = df.style.applymap(color_red)
st = s.render()

print (st)
<style  type="text/css" >
#T_c7738_row0_col0,#T_c7738_row0_col1,#T_c7738_row1_col1{
            color:  black;
        }#T_c7738_row1_col0{
            color:  red;
        }</style><table id="T_c7738_" ><thead>    <tr>        <th class="blank level0" ></th>        <th class="col_heading level0 col0" >a</th>        <th class="col_heading level0 col1" >b</th>    </tr></thead><tbody>
                <tr>
                        <th id="T_c7738_level0_row0" class="row_heading level0 row0" >0</th>
                        <td id="T_c7738_row0_col0" class="data row0 col0" >None</td>
                        <td id="T_c7738_row0_col1" class="data row0 col1" >c</td>
            </tr>
            <tr>
                        <th id="T_c7738_level0_row1" class="row_heading level0 row1" >1</th>
                        <td id="T_c7738_row1_col0" class="data row1 col0" >aa|ss</td>
                        <td id="T_c7738_row1_col1" class="data row1 col1" >None</td>
            </tr>
    </tbody></table>
1
hpaulj On

Your function color_red works ok if val is a string, but if it is None (as in your 'd' column):

In [26]: '|' in '12|34'
Out[26]: True
In [27]: '|' in None
Traceback (most recent call last):
  File "<ipython-input-27-aa625f00502f>", line 1, in <module>
    '|' in None
TypeError: argument of type 'NoneType' is not iterable

Either avoid passing that column (or rows) to function, or write the function so that it does not choke when given a None argument.

It's hard to tell from the df display whether the cell values like "2" are strings or integers. "1--2" would be strings. Conceivably the "None" cells could also be strings, but the error indicates they are real None objects (which are not strings).