Jun-05-2021, 12:30 PM
Please consider the following Python code:
import pandas as pd data = { "l1": [50, 100, 150, 200, 250, -350, 450], "l2": [100, 120, 150, 190, 240, 200, 500], } #load data into a DataFrame object: df = pd.DataFrame(data)I want to write code to add a new column to the data frame df where the new values are the larger of the values in l1 and l2. I can do this using a loop with the following code:
maxValue = [] limit = len ( df ) for i in range( limit ): if df.l1[i] > df.l2[i]: maxValue.append( df.l1[i] ) else: maxValue.append( df.l2[i] ) df['l3'] = maxValueHowever, I believe there should be a better way. I tired this:
df['l3'] = df['l1'] if df['l1'] > df['l2'] else df['l2']This produced the following error message:
Error: File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__
raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
What is the right way to add the new column l3 to df?