83demon
83demon

Reputation: 47

How to get initial row's indexes from df.groupby?

Actually, I have df

print(df):

       date  value   other_columns
0      1995  5
1      1995  13
2      1995  478

and so on...

After grouping them by date df1 = df.groupby(by='date')['value'].min() I wonder how to get initial row's index. In this case, I want to get integer 0, because there was the lowest value in 1995. Thank you in advance.

Upvotes: 1

Views: 1251

Answers (2)

theletz
theletz

Reputation: 1805

I think this what you mean:

Actually, you want the original dataframe with only the rows with minimal value across each group. For this you can use pandas transform method:

>>> df = pd.DataFrame({'date' : [1995, 1995, 1995, 2000, 2000, 2000], 'value': [5, 13, 478, 7, 1, 8]})
>>> df
   date  value
0  1995      5
1  1995     13
2  1995    478
3  2000      7
4  2000      1
5  2000      8

>>> minimal_value = df.groupby(['date'])['value'].transform(min)
>>> minimal_value
0    5
1    5
2    5
3    1
4    1
5    1
Name: value, dtype: int64

Now you can use this to get only the relevant rows:

>>> df.loc[df['value'] == minimal_value]
   date  value
0  1995      5
4  2000      1

Upvotes: 1

Renaud
Renaud

Reputation: 2819

You have to create a Column with the index value before doing the groupby:

df['initialIndex'] = df.index.values
#do the groupby

Upvotes: 1

Related Questions