Re: [Numpy-discussion] select column based on another column value
Hi I have the following array and want to calculate the average per day. Import numpy as np from datetime import date today = date(today.year, today.month, 9) yesterday = date(today.year, today.month, 8) twodaysago = date(today.year, today.month, 7) testdata = np.array([[today, "r", 3.2],[today, "r", 4.3],[today, "r", 5.3],[yesterday, "r", 6.3],[yesterday, "r", 9.3],[yesterday, "r", 1.3],[twodaysago, "r", 3.3],[twodaysago, "r", 1.2],[twodaysago, "r", 4.3]]) which produces array([[datetime.date(2013, 10, 9), 'r', 3.2], [datetime.date(2013, 10, 9), 'r', 4.3], [datetime.date(2013, 10, 9), 'r', 5.3], [datetime.date(2013, 10, 8), 'r', 6.3], [datetime.date(2013, 10, 8), 'r', 9.3], [datetime.date(2013, 10, 8), 'r', 1.3], [datetime.date(2013, 10, 7), 'r', 3.3], [datetime.date(2013, 10, 7), 'r', 1.2], [datetime.date(2013, 10, 7), 'r', 4.3]], dtype=object) And I want the output as (numbers aren't correct below): 2013-10-09, 4.54 2013-10-08, 5.43 2013-10-07, 2.76 But not sure how. Thanks Roelf Tibra Trading Europe Limited is Authorised and Regulated by the FCA (Company No. 06061713) Tibra Global Services Pty Limited London Branch (Company No. BR014370) Tibra Equities Europe Limited (Company No. 07283912) Registered in England & Wales - Level 12, 5 Aldermanbury Square London EC2V 7HR Unless otherwise stated, this communication is neither an offer nor solicitation of an offer to sell or purchase any investment The contents of this email including any attachments are confidential. If you have received this email in error, please advise the sender by return email and delete this email. Any unauthorised use of the contents of the email is prohibited and you must not disseminate, copy or distribute the message or use the information contained in the email or its attachments in any way. The views or opinions expressed are the author's own and may not reflect the views or opinions of Tibra. Tibra does not guarantee the integrity of any emails or attached files. E-mails may be interfered with, may contain computer viruses or other defects. Under no circumstances do we accept liability for any loss or damage which may result from your receipt of this message or any attachments.
I think you need this
testdata[testdata[:,0]==today][:,2].mean() 4.266666666666667
testdata[testdata[:,0]==yesterday][:,2].mean() 5.633333333333334
testdata[testdata[:,0]==twodaysago][:,2].mean() 2.9333333333333336
On 10/09/2013 06:46 AM, Roelf Schreurs wrote:
Hi
I have the following array and want to calculate the average per day.
Import numpy as np
from datetime import date
today = date(today.year, today.month, 9)
yesterday = date(today.year, today.month, 8)
twodaysago = date(today.year, today.month, 7)
testdata = np.array([[today, "r", 3.2],[today, "r", 4.3],[today, "r", 5.3],[yesterday, "r", 6.3],[yesterday, "r", 9.3],[yesterday, "r", 1.3],[twodaysago, "r", 3.3],[twodaysago, "r", 1.2],[twodaysago, "r", 4.3]])
which produces
array([[datetime.date(2013, 10, 9), 'r', 3.2],
[datetime.date(2013, 10, 9), 'r', 4.3],
[datetime.date(2013, 10, 9), 'r', 5.3],
[datetime.date(2013, 10, 8), 'r', 6.3],
[datetime.date(2013, 10, 8), 'r', 9.3],
[datetime.date(2013, 10, 8), 'r', 1.3],
[datetime.date(2013, 10, 7), 'r', 3.3],
[datetime.date(2013, 10, 7), 'r', 1.2],
[datetime.date(2013, 10, 7), 'r', 4.3]], dtype=object)
And I want the output as (numbers aren't correct below):
2013-10-09, 4.54
2013-10-08, 5.43
2013-10-07, 2.76
But not sure how.
Thanks
*Roelf *
Tibra Trading Europe Limited is Authorised and Regulated by the FCA (Company No. 06061713) Tibra Global Services Pty Limited London Branch (Company No. BR014370) Tibra Equities Europe Limited (Company No. 07283912) Registered in England & Wales - Level 12, 5 Aldermanbury Square London EC2V 7HR
Unless otherwise stated, this communication is neither an offer nor solicitation of an offer to sell or purchase any investment
The contents of this email including any attachments are confidential. If you have received this email in error, please advise the sender by return email and delete this email. Any unauthorised use of the contents of the email is prohibited and you must not disseminate, copy or distribute the message or use the information contained in the email or its attachments in any way.
The views or opinions expressed are the author's own and may not reflect the views or opinions of Tibra. Tibra does not guarantee the integrity of any emails or attached files. E-mails may be interfered with, may contain computer viruses or other defects. Under no circumstances do we accept liability for any loss or damage which may result from your receipt of this message or any attachments.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
That does work thanks. And in a for loop was my goal, so this works for me. for i in np.unique(testdata[:,0]): print(i, testdata[testdata[:,0]==i][:,2].mean()) (datetime.date(2013, 10, 7), 2.9333333333333336) (datetime.date(2013, 10, 8), 5.633333333333334) (datetime.date(2013, 10, 9), 4.266666666666667) Roelf From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Georgios Exarchakis Sent: 09 October 2013 15:22 To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] select column based on another column value I think you need this
testdata[testdata[:,0]==today][:,2].mean() 4.266666666666667
testdata[testdata[:,0]==yesterday][:,2].mean() 5.633333333333334
testdata[testdata[:,0]==twodaysago][:,2].mean() 2.9333333333333336
On 10/09/2013 06:46 AM, Roelf Schreurs wrote: Hi I have the following array and want to calculate the average per day. Import numpy as np from datetime import date today = date(today.year, today.month, 9) yesterday = date(today.year, today.month, 8) twodaysago = date(today.year, today.month, 7) testdata = np.array([[today, "r", 3.2],[today, "r", 4.3],[today, "r", 5.3],[yesterday, "r", 6.3],[yesterday, "r", 9.3],[yesterday, "r", 1.3],[twodaysago, "r", 3.3],[twodaysago, "r", 1.2],[twodaysago, "r", 4.3]]) which produces array([[datetime.date(2013, 10, 9), 'r', 3.2], [datetime.date(2013, 10, 9), 'r', 4.3], [datetime.date(2013, 10, 9), 'r', 5.3], [datetime.date(2013, 10, 8), 'r', 6.3], [datetime.date(2013, 10, 8), 'r', 9.3], [datetime.date(2013, 10, 8), 'r', 1.3], [datetime.date(2013, 10, 7), 'r', 3.3], [datetime.date(2013, 10, 7), 'r', 1.2], [datetime.date(2013, 10, 7), 'r', 4.3]], dtype=object) And I want the output as (numbers aren't correct below): 2013-10-09, 4.54 2013-10-08, 5.43 2013-10-07, 2.76 But not sure how. Thanks Roelf Tibra Trading Europe Limited is Authorised and Regulated by the FCA (Company No. 06061713) Tibra Global Services Pty Limited London Branch (Company No. BR014370) Tibra Equities Europe Limited (Company No. 07283912) Registered in England & Wales - Level 12, 5 Aldermanbury Square London EC2V 7HR Unless otherwise stated, this communication is neither an offer nor solicitation of an offer to sell or purchase any investment The contents of this email including any attachments are confidential. If you have received this email in error, please advise the sender by return email and delete this email. Any unauthorised use of the contents of the email is prohibited and you must not disseminate, copy or distribute the message or use the information contained in the email or its attachments in any way. The views or opinions expressed are the author's own and may not reflect the views or opinions of Tibra. Tibra does not guarantee the integrity of any emails or attached files. E-mails may be interfered with, may contain computer viruses or other defects. Under no circumstances do we accept liability for any loss or damage which may result from your receipt of this message or any attachments. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.orgmailto:NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion Tibra Trading Europe Limited is Authorised and Regulated by the FCA (Company No. 06061713) Tibra Global Services Pty Limited London Branch (Company No. BR014370) Tibra Equities Europe Limited (Company No. 07283912) Registered in England & Wales - Level 12, 5 Aldermanbury Square London EC2V 7HR Unless otherwise stated, this communication is neither an offer nor solicitation of an offer to sell or purchase any investment The contents of this email including any attachments are confidential. If you have received this email in error, please advise the sender by return email and delete this email. Any unauthorised use of the contents of the email is prohibited and you must not disseminate, copy or distribute the message or use the information contained in the email or its attachments in any way. The views or opinions expressed are the author's own and may not reflect the views or opinions of Tibra. Tibra does not guarantee the integrity of any emails or attached files. E-mails may be interfered with, may contain computer viruses or other defects. Under no circumstances do we accept liability for any loss or damage which may result from your receipt of this message or any attachments.
participants (2)
-
Georgios Exarchakis
-
Roelf Schreurs