0

Okay, I edited this question with a new example.

I can't fill my new column df['Colour'] in my dataframe.

#I used an example of one of the answers:

d = {"symbol":["Heart", "Heart", "Diamond", "Spade"], "symbol2":["Heart","Diamond", "Spade", "Spade"]}`

df_s = pd.DataFrame(d)


df_s['color'] = df_s['symbol'].map(lambda x: 'Correct' if x == df_s['symbol2'] else 'Wrong')

With this code, I want to create a new column called 'Colour' and fill the blanks with the values with 'Correct' or 'Wrong'.

But I only get the error:

Error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What did I do wrong?

Edit: The Error stays still the same.

booklover
  • 43
  • 6

3 Answers3

0

Let's assume your data-frame has the following structure:

d = {"symbol":["Heart", "Heart", "Diamond", "Spade"]}
df = pd.DataFrame(d)
print(df)

    symbol
0    Heart
1    Heart
2  Diamond
3    Spade

You can use .map on the 'symbol' column to achieve the desired result:

df['color'] = df['symbol'].map(lambda x: 'red' if x == 'Heart' else 'not red')
print(df)

    symbol    color
0    Heart      red
1    Heart      red
2  Diamond  not red
3    Spade  not red

Edit

based on the new edit of the OP, in case one wish to consider multiple columns condition its best to use .apply row-wise:

df_s['color'] = df_s.apply(lambda r: 'red' if r[0]=='Heart' and r[1]=='Heart' else 'not red', axis=1)

To check if both columns are the same you can do (with the same logic):

df_s.apply(lambda r: 'correct' if r[0]==r[1] else 'wrong', axis=1)

In case you want to check all the column or more then 2:

df_s.apply(lambda r: 'correct' if len(set(r)) == 1 else 'wrong', axis=1)

If all the columns in a row are the same then set() of the row will have length 1.

David
  • 8,113
  • 2
  • 17
  • 36
  • I tried your code, but it wasn't working in my case. Do you have to change something if your condition is two columns to be the same? So if the two columns it should say 'True' and if not 'False'. I got the same Error. – booklover Oct 23 '20 at 12:39
  • @booklover so maybe share a sample of your data-frame. I don't understand why it doesn't work in your specific case. – David Oct 23 '20 at 12:40
  • I edited the question. I am sorry, I couldn't make the code format, but you should see it, if you put the code in your editor. – booklover Oct 23 '20 at 12:59
  • @booklover see my edit, it now works with the new added example – David Oct 23 '20 at 13:10
  • But this solves the problem only if the symbol is Heart. If other symboles are the same, it is diffrent. – booklover Oct 23 '20 at 13:14
  • @booklover you want to check whether 2 column has the same value? – David Oct 23 '20 at 13:17
  • Yeah, right and if they do I want to have a third column, which says right and wrong. – booklover Oct 23 '20 at 14:29
  • @booklover see my edit – David Oct 23 '20 at 14:31
  • Okay, thank you very much, this is working and I get a result. Here I have to compare numbers und it marks some equal Numbers as 'Wrong'. It is a little bit strange, but at least I get a result. Thank you. :) – booklover Oct 23 '20 at 16:19
0

The problem is that you want to look at a value in each row and if the value in that row is equal to "Heart", you want to do something with it. However, what you're doing is that you look at the entire series and python does not know what to do with that (when is it true and should be done? If all elements are true (a.all()) or at least one (a.any()) or ...).

To achieve what you asked for, you can do for example:

df.loc[df['symbole'] == 'Heart', 'Colour'] = 'red'
df.loc[df['symbole'] != 'Heart', 'Colour'] = 'not red'

For more ways how to do it, see those answers:

Using conditional to generate new column in pandas dataframe

Pandas conditional creation of a series/dataframe column

My Work
  • 2,143
  • 2
  • 19
  • 47
0

You can also use pandas apply:

df['color'] = df.apply(lambda row: (   
                                      'red'
                                       if row['symbole'] == 'Heart'
                                       else 'not red',
                                       axis=1
                                   )
MarkL
  • 97
  • 7
  • Thank you I tried it, but it didn't work as well. I changed the question a bit. Could it be, because I want to compare two columns instead of the value of one column? – booklover Oct 23 '20 at 13:06