I have a dataframe includes two columns [Color, Value] as shown in the figure below:
df1 = pd.DataFrame({"Color":[Green, Blue, Green, Green, Blue, Red, Green, Green, Blue, Red, Green, Green, Blue, Red, Blue, Blue, Green],
"Value":[20, 21, 25, 30, 41, 512, 40, 41, 352, 31, 52, 451, 253, 54, 142, 122, 784 ]})
The aim is to find a list of duplications of color's column and count them. I used this code:
dups_colors = pd.concat(g for _, g in data.groupby("Color") if len(g) > 1)#find duplications
The output is:
Then, I counted the duplications using this code:
count = dups_colors.pivot_table(index=['Color'], aggfunc='size')#count number of duplications
The output is:
Until here everything is fine. Now I have two questions as follows:
Q1: I need to add the count column into the dups_colors dataframe in this mannar.
Q2: I need to extract the values correspond to each color into a sperate column with header of the color, same like this:
Please have a look and help me.