[Solved] Find the length of each group and use it inside a for loop in pandas

John Asks: Find the length of each group and use it inside a for loop in pandas
I am attempting to determine a size for each group. However, I’m having trouble getting it to return the size of each group in a for loop.

Code:
grouped = df1.groupby('Ids')
size=grouped.size()
for g in grouped.groups:
    group = grouped.get_group(g)
    train_size = int(0.80 * size)
    train=df1.iloc[:train_size]
    test=df1.iloc[train_size:]

For example, if there are three ids (A,B, and C), train size should provide 80 percent of the length for each id based on their size.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.