Jalo89
Jalo89

Reputation: 9

Find identical values in a column of a dataframe and create a new dataframe with each duplicate

I'm relatively new to Python and searching for an answer to my problem the whole day already, but unfortunately could't find anything.

I import data to python from an excel file and create a dataframe with it. The Data is a long list with customers and their payments. The column Account (which represents the customer as their customer number) has many duplicates, since the customers usually buy not only one product.

I would like to look through that Account column, take all identical Account numbers and create a new dataframe with all these rows. That for example I have a new Dataframe with all the rows which had account number 10000, the next dataframe with 10001 and so on.

Can somebody help me with that? Thanks in advance!

Upvotes: 1

Views: 72

Answers (1)

Gary
Gary

Reputation: 899

If I understood you correctly, something like this will help. Let's assume df_customer is your dataframe.

Code:

cntr=1
for i in df_customer['Account'].unique():
    locals()['df_customer'+str(cntr)]=df_customer[df_customer['Account'] == i]
    cntr += 1

What the above code will do is create a new dataframe (df_customer1,df_customer2,df_customer3 etc) for every account number in the dataframe.

To check the number of dataframes created, you can check the final value of cntr.

Do let me know if this is what you intended to do!

Upvotes: 1

Related Questions