-1

I need to create a new column filled with the not null data from other 3 columns.

This is the expected output:

Expected output

This is what I tried so far

df['color'] = df['d', 'f', 'g'].apply(lambda x: int(x) if x>=0 else 'Nope')

But I have this key error:

KeyError: ('d', 'f', 'g')
Timus
  • 10,974
  • 5
  • 14
  • 28
  • [Please do not upload images of data when asking a question](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). Provide a MRE (see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/14311263)) instead. – Timus Dec 23 '22 at 16:01
  • Also don't correct your question in a comment, [edit](https://stackoverflow.com/posts/74898290/edit) your question instead. (I've done it for you.) – Timus Dec 23 '22 at 16:02
  • Apart from the key error that is easily fixed: The function `lambda x: int(x) if x>=0 else 'Nope'` doesn't seem to be related to your goal? How would `int("Yellow")` work, for example? – Timus Dec 23 '22 at 16:15

1 Answers1

0

The correct syntax to select multiple columns (named a, b, &c in your case) is:

df[['a', 'b', 'c']]

LITzman
  • 730
  • 1
  • 16
  • Sorry,but it did't help. Now I get this error: TypeError: Unordered Categoricals can only compare equality or not. I forgot to mention that data on d, f and g is Categorical – Roberto Mulé Dec 23 '22 at 10:06