4

I have a column which has the ticketID for a show,(each family member uses the same ticketID ) i want to create a new cloumn which is family size by counting how many times the ticketID is repeated.

ticketID
113796
2543
19950
382653
349211
3101297
PC 17562
113503
113503
  • 2
    On Stack Overflow you need to show what attempts you have made. Please read http://stackoverflow.com/help/how-to-ask for some pointers. We won't just write code for you – Mikkel Dec 25 '16 at 11:46
  • 1
    [`df.ticketID.values_counts()`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.value_counts.html) – piRSquared Dec 25 '16 at 11:49

2 Answers2

6

try this:

In [123]: df = pd.DataFrame({'ticketID':np.random.randint(0, 3, 5)})

In [124]: df
Out[124]:
   ticketID
0         1
1         2
2         1
3         1
4         2

In [125]: df['family_size'] = df.ticketID.map(df.ticketID.value_counts())

In [126]: df
Out[126]:
   ticketID  family_size
0         1            3
1         2            2
2         1            3
3         1            3
4         2            2
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
2

You could use transform

In [152]: df
Out[152]:
   ticketID
0         1
1         2
2         1
3         1
4         2

In [153]: df['family_size'] = df.groupby('ticketID')['ticketID'].transform('size')

In [154]: df
Out[154]:
   ticketID  family_size
0         1            3
1         2            2
2         1            3
3         1            3
4         2            2
Zero
  • 74,117
  • 18
  • 147
  • 154