0

This may not be a big problem, I just haven't noticed this output of None before when doing .apply()

Toy example:

mydf = pd.DataFrame({'col1':['test1',np.nan,'test3','test4'],
                   'col2':['test5','test6','test7','test8']})

mydf


    col1   col2
0  test1  test5
1    NaN  test6
2  test3  test7
3  test4  test8

Function to just add the values together in a string:

def myfunc(row):


    thing = str(row['col1']) + str(row['col2'])

    print(thing)

Applying it:

mydf.apply(myfunc,axis=1)

My output:

test1test5
nantest6
test3test7
test4test8

0    None
1    None
2    None
3    None
dtype: object

Is it something to be worried about? I will be applying something like this to some real data shortly. I am doing this in Jupyter Notebook if it makes a difference.

SCool
  • 3,104
  • 4
  • 21
  • 49

2 Answers2

3

You should return the string instead of printing it.

Chris K
  • 467
  • 5
  • 11
0

user return not print before applying to df..

>>> mydf
    col1   col2
0  test1  test5
1    NaN  test6
2  test3  test7
3  test4  test8


>>> def myfunc(row):
...   thing = str(row['col1']) + str(row['col2'])
...   return thing
...

>>> mydf.apply(myfunc,axis=1)
0    test1test5
1      nantest6
2    test3test7
3    test4test8
dtype: object

Just an idea of doing it other way around ..

>>> cols = ['col1', 'col2']
>>> mydf[cols].astype(str).sum(axis=1)
0    test1test5
1      nantest6
2    test3test7
3    test4test8
dtype: object
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53