Say there is a dataframe with 100 records containing 4(or n) columns, example of dataframe below:
id target col3 col4 00 0 .. .. 00 0 .. .. 00 0 .. .. 01 1 .. .. 01 1 .. .. 01 0 .. .. 01 1 .. .. 02 1 .. .. 02 0 .. .. 02 1 .. .. 02 0 .. .. .. .. Based on this dataframe I want to create a new dataframe that is a resultant of group_by on this dataframe and value_counts of a specific column (target).
I have figured out how to get those values(my current code):
for id, target in df.group_by('id'): print(id) print(group.target.value_counts()) Which give me the following output:
00 0 3 Name: target, dtype: int64 01 0 1 1 3 Name: target, dtype: int64 02 0 2 1 2 Name: target, dtype: int64 .. .. I am able to get these values but I can't seem to pass these values into a empty dataframe. I would like to create a new dataframe that represents this information in this format:
id 0 1 00 3 NaN 01 1 3 02 2 2 .. ..
df.groupby('id').count().reset_index()