1
$\begingroup$

my array is looking like this

a=np.array([[ 25,  29,  19,  93],
       [ 27,  59,  23,  345],
       [ 24,  426,  15,  593],
       [ 24,  87,  50.2, 139],
       [ 13,  86,  12.4, 139],
       [ 13,  25,  85, 142],
       [ 62,  62,  68.2, 182],
       [ 27,  25,  20, 150],
       [ 25,  53,  71, 1850],
       [ 64,  67,  21.1, 1570],
       [ 64,  57,  73, 1502]])

i want to return the lowest value of column 2 based on the unique value of column 0. column 0 should contain unique values. I tries the following code, but was not giving me the exact result. Can some one help me to sort out this? thanks

sidx = np.lexsort(a[:,[2,0]].T)
dx = np.append(np.flatnonzero(a[2:,0] >a[:-2,0]), a.shape[0]-1) 
result = a[sidx[idx]]
print result

I want to get result like

[25...
 27
 24
 13
 62
 64...]
a=[[196512 28978 Decimal('12.7805170314276')]
 [196512 34591 Decimal('12.8994111000000')]
 [196512 13078 Decimal('12.9135746000000')]
 [196641 114569 Decimal('12.9267705000000')]
 [196641 118910 Decimal('12.8983353775637')]
 [196641 100688 Decimal('12.9505091000000')]]this is a big list
i used,
df = pd.DataFrame(a)
df.columns = ['a','b','c']
df.index = df.a.astype(str) 
dd=df.groupby('a').min()['c']

but i am getting,

195556    12.7805170314276
195937    12.7805170314276
196149    12.7805170314276
196152    12.7805170314276
196155    12.7805170314276
196262    12.7805170314276

$\endgroup$
1
$\begingroup$

Here's an easy solution. The sort order changes, but that shouldn't be difficult to address if you really care:

import pandas as pd

df = pd.DataFrame(a)
df.columns = ['a','b','c','d']
df.index = df.a.astype(str) # to preserve correspondence
df.groupby('a').min()['b']

a
13.0    25.0
24.0    87.0
25.0    29.0
27.0    25.0
62.0    62.0
64.0    57.0
Name: b, dtype: float64

Edit: I think you meant to name your array y instead of a. This works for me:

 from decimal import Decimal

 y=np.array([[196512, 28978, Decimal('12.7805170314276')], 
    [196512, 34591, Decimal('12.8994111000000')] ,
    [196512, 13078, Decimal('12.9135746000000')] ,
    [196641, 114569, Decimal('12.9267705000000')] ,
    [196641, 118910, Decimal('12.8983353775637')] ,
    [196641, 100688, Decimal('12.9505091000000')]])


 df = pd.DataFrame(y) 
 df.columns = ['a','b','c'] 
 df.index = df.a.astype(str) 
 dd=df.groupby('a').min()['c'] 

In [210]: dd
Out[210]:
a
196512    12.7805170314276
196641    12.8983353775637
Name: c, dtype: object
$\endgroup$
  • $\begingroup$ when i changed the dataframe to 3 column <pre>[[196512 28978 Decimal('12.7805170314276')] [196512 34591 Decimal('12.8994111000000')] [196512 13078 Decimal('12.9135746000000')] [196641 114569 Decimal('12.9267705000000')] [196641 118910 Decimal('12.8983353775637')] [196641 100688 Decimal('12.9505091000000')]] </pre> it is resulting like <pre>195556 12.7805170314276 195937 12.7805170314276 196149 12.7805170314276 196152 12.7805170314276.....</pre> it is taking the minimum of that entire column value, not for the group min. it assign 12.7805170314276 for all column $\endgroup$ – Sam Joe Jan 4 '18 at 13:12
  • $\begingroup$ Edit your post demonstrating a reproducible example of the issue you're having. I can't really tell what's going on in that comment. $\endgroup$ – David Marx Jan 4 '18 at 13:16
  • $\begingroup$ reproducible. like your original post. Give me a data strucutre I can copy paste and a code chunk that causes the unwanted behavior you're experiencing. $\endgroup$ – David Marx Jan 4 '18 at 13:21
  • $\begingroup$ I think you meant to name that array y instead of a, which will just give you: 196512 12.7805170314276 196641 12.8983353775637 $\endgroup$ – David Marx Jan 4 '18 at 13:35
  • $\begingroup$ David i edited in the question, with code, can you check it. $\endgroup$ – Sam Joe Jan 4 '18 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.