2

I am using apply on my data frame my_df like below:

my_df['column_C'] = my_df.apply(lambda x : 'hello' if x['column_B'] is None else x['column_B'] )

I want:

  if x['column_B'] = None -> return 'hello'
  if x['column_B'] != None -> return x['column_B']

Then I got the following errors:

<ipython-input-31-aa087c9a635e> in <lambda>(x)
----> 1 my_df['column_C'] = my_df.apply(lambda x : 'hello' if x['column_B'] is None else x['column_B'] )

/usr/local/lib/python3.4/dist-packages/pandas/core/series.py in __getitem__(self, key)
    599         key = com._apply_if_callable(key, self)
    600         try:
--> 601             result = self.index.get_value(self, key)
    602 
    603             if not is_scalar(result):

/usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_value(self, series, key)
   2187             # python 3
   2188             if is_scalar(key):  # pragma: no cover
-> 2189                 raise IndexError(key)
   2190             raise InvalidIndexError(key)
   2191 

IndexError: ('column_B', 'occurred at index column_A')

Does anyone know what I did wrong here?

2
  • 3
    why not: my_df['column_C'] = my_df['column_C'].fillna('hello')? Commented Nov 15, 2016 at 21:36
  • This is certainly a smarter approach thanks! Still curious about what was wrong in the apply function though ... Commented Nov 15, 2016 at 21:39

1 Answer 1

2

You need to apply specifying axis=1 to apply it to each row, not each column. See the documentation on DataFrame.apply:

axis : {0 or 'index', 1 or 'columns'}, default 0

* 0 or 'index': apply function to each column
* 1 or 'columns': apply function to each row

In your current call, it cannot find x['column_B'] when it's really using a pd.Series that corresponds to column_A.

So if you use the following it will work.

my_df['column_C'] = my_df.apply(lambda x : 'hello' 
                                if x['column_B'] is None
                                else x['column_B'], axis=1)

Note: as pointed in the comment above, DataFrame.fillna is more appropriate for this task.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.