1

In pandas I have created a dataframe from importing from CSV.

data = pd.read_csv('pathToCSV', index_col=[0],usecols=[0,2,3],names=['Date','High','Low'])

Output looks like this-

2009.09.18  112  111
2009.09.19  114  222

Now what if I want to calculate an average of the two columns, row by row, and then add the value as a new column? What I did was

average = (data[1]+data[2])/2

Then

data.join(average)

But I get an error! I am doing this correct?

2
  • What error do you get? Commented Nov 27, 2018 at 3:25
  • File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 5325, in _join_compat raise ValueError('Other Series must have a name') ValueError: Other Series must have a name Commented Nov 27, 2018 at 3:50

2 Answers 2

1

I think you're just looking for the mean function, which you want to apply row-wise (axis = 1) to the columns High and Low:

# starting with frame:
>>> data
         Date  High  Low
0  2009.09.18   112  111
1  2009.09.19   114  222

# Use:
data['row_average'] = data[['High','Low']].mean(axis=1)

# and you end up with the frame:
>>> data
         Date  High  Low  row_average
0  2009.09.18   112  111        111.5
1  2009.09.19   114  222        168.0
Sign up to request clarification or add additional context in comments.

1 Comment

And thanks for the explanation of "row-wise (axis = 1)" much appreciated!
0

While sacul is absolutely correct, you may consider the following solution as well.

def avg(x,y):
    return (x+y)/2
df['row_avg'] = df[['High','Low']].apply(lambda x:avg(*x),axis=1) 

The advantage of this approach is you can define any function and then apply to DataFrame rows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.