0

Wondering what sort of group by I can use to produce this result.

Input:

    Root    Strike
33  AAL     49
43  AAL     49
44  AAL     49.5
35  AAL     50
45  AAL     50

Output

    Strike Num  Root    Strike
33  1           AAL     49
43  1           AAL     49
44  2           AAL     49.5
35  3           AAL     50
45  3           AAL     50
smci
  • 32,567
  • 20
  • 113
  • 146
nicholas.reichel
  • 2,260
  • 7
  • 20
  • 28
  • You want to create a new column 'Strike Num' ranking rows by Strike – smci May 12 '18 at 00:32
  • In fact it's a duplicate of [python pandas rank by column](https://stackoverflow.com/questions/17604665/python-pandas-rank-by-column) – smci May 12 '18 at 00:45

1 Answers1

1

You don't need a group-by. You want to create a new column 'Strike Num' ranking rows by Strike. pandas rank() automatically does the grouping, ordering, and gives you control over how ties are indexed; here you want the rank to be the smallest: method='min'

df = pd.read_csv(pd.compat.StringIO(data), index_col=0, sep='\s+')

df.insert(0, 'Strike Num', df.Strike.rank(method='min').astype(int))

    Strike Num Root  Strike
33           1  AAL    49.0
43           1  AAL    49.0
44           3  AAL    49.5
35           4  AAL    50.0
45           4  AAL    50.0
smci
  • 32,567
  • 20
  • 113
  • 146