• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Using lamda to compare two columns

Resolved: Using lamda to compare two columns

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

My dataframe is like this: df = pd.DataFrame({'A': [1,2,3], 'B': [1,4,5]})
If column A has the same value as column B, output 1, else 0.
I want to output like this:
I figured out df['is_equal'] = np.where((df['A'] == df['B']), 1, 0) worked fine.
But I want to use lambda here because I used a similar line in another case before. df['is_equals'] = df.apply(lambda x: 1 if df['A']==1 else 0, axis=1) won’t work. It threw the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). Why did this error happen and how can I fix the code.
Thank you in advance.

Answer:

I also agree with DYZ’s opinion. But if you want to use .apply anyway, I can suggest something like this.

If you have better answer, please add a comment about this, thank you!

dataframe pandas python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: TYPO3 SQL error: Field ‘tx_imagezoom_set’ doesn’t have a default value

26/03/2023

Resolved: std::regex_replace to replace multiple combinations

26/03/2023

Resolved: How can I copy files using the ansible.builtin.copy module and avoid conflicting file names?

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.