Python Forum

Full Version: Adding a new column to a Panda Data Frame
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'] = maxValue
However, 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?
with numpy, like ifesle statment in R...
df['l3'] = np.where(df['l1'] > df['l2'], df['l1'],  df['l2'])
Alternatively
df['l3'] = df.apply(lambda row: max(row.l1,row.l2), axis=1)
Output:
l1 l2 l3 0 50 100 100 1 100 120 120 2 150 150 150 3 200 190 200 4 250 240 250 5 -350 200 200 6 450 500 500