Skip to content Skip to sidebar Skip to footer

Extract DataFrame From Duplicated Values

I've a DataFrame with a column in which are stored more duplicates related to different data. I don't know the number of duplicates in A and who are they, but I need to extract n-

Solution 1:

TRY:

df_list = [k for _,k in df.groupby('A')]

OUTPUT:

[     A    B
 1  120  abc
 5  120  def,
      A    B
 2  121  def
 4  121  abc
 6  121  def
 8  121  ghi,
      A    B
 3  122  ghi
 7  122  abc]

Use the below code if you also want to reset the index of each dataframe.

df_list = [k.reset_index(drop=True) for _,k in df.groupby('A')]

You can use dict comprehension if you need group_names:

df_dict = {g:k.reset_index(drop=True) for g,k in df.groupby('A')}

Dict output:

{120:      A    B
0  120  abc
1  120  def,
 121:      A    B
0  121  def
1  121  abc
2  121  def
3  121  ghi,
 122:      A    B
0  122  ghi
1  122  abc}

Post a Comment for "Extract DataFrame From Duplicated Values"