I'm still not accustomed to using functions, so I decided to try to create one (for practice) that prints out the first five groups of a groupby object. The problem with the function I have written is that, it seems to be printing out all of the groups of the groupby object, instead of just the first five. I can't figure out the error.
x = Ticket_Names.groupby('Ticket')
def Groupby_func(y):
a=0
while a <=5: #trying to use 'a' as a limiter,
#to limit printing just the first five groups
for i, j in y:
print i,j
a+=1
Groupby_func(x) # calling the function
So instead of printing just the first five groups, it's printing all of them (around 238).
My dataframe looks something this:
Ticket Name
PassengerId
258 110152 Cherry, Miss. Gladys
505 110152 Maioni, Miss. Roberta
760 110152 Lucy, Noel Martha Dye
586 110413 Taussig, Miss. Ruth
263 110413 Taussig, Mr. Emil
737 6608 Ford, Mrs. Edward
93 5734 Chaffee, Mr. Herbert
906 5734 Chaffee, Mrs. Herbert
746 5735 Crosby, Capt. Edward Gifford
541 5735 Crosby, Miss. Harriet
The groupby groups them by ticket, so in this sample set only 5-6 groups will be created, but in the full dataframe around 230-300 groups are created.
When I run the function above, instead of getting it print the first five groups, it's printing what seems to be all the groups of the groupby object.