1

Can anybody explain me why this code should not work?

wordsCount = {}

def addWord(x):
    print(x)

df.apply(addWord(x))

It return the error: TypeError: ("'NoneType' object is not callable", 'occurred at index 0') The dataframe df contains some None value in some cell. My intention is to apply a function to all non-None value.

user1315621
  • 2,548
  • 6
  • 31
  • 60
  • `.apply()` expects a function, but you are passing it the return value by calling `addWord(x)`, which is None, since no return value is specified in the `addWord` function. Just pass `addWord` without the parenthesis. – Craig Oct 16 '17 at 01:21
  • Ok thanks. So now I discover than with apply I can only iterate over columns. What is I want to iterate over each element of the dataframe? – user1315621 Oct 16 '17 at 01:33
  • https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.applymap.html – Craig Oct 16 '17 at 01:34

1 Answers1

3

Use pandas.DataFrame.applymap

df.applymap(addWord)
piRSquared
  • 265,629
  • 48
  • 427
  • 571