Antonio López Ruiz
Antonio López Ruiz

Reputation: 1466

Column to Transacction ID for association rules on dataframes from Pandas Python.

I imported a CSV into Python with Pandas and I would like to be able to use one as the columns as a transaction ID in order for me to make association rules.

(link: https://github.com/antonio1695/Python/blob/master/nearBPO/facturas.csv)

I hope someone can help me to:

Use UUID as a transaction ID for me to have a dataframe like the following:

UUID     Desc
123ex    Meat,Beer

In order for me to get association rules like: {Meat} => {Beer}.

Also, a recommendation on a library to do so in a simple way would be appreciated.

Thank you for your time.

Upvotes: 2

Views: 657

Answers (1)

EdChum
EdChum

Reputation: 394041

You can aggregate values into a list by doing the following:

df.groupby('UUID')['Desc'].apply(list)

This will give you what you want, if you want the UUID back as a column you can call reset_index on the above:

df.groupby('UUID')['Desc'].apply(list).reset_index()

Also for a Series you can still export this to a csv same as with a df:

df.groupby('UUID')['Desc'].apply(list).to_csv(your_path)

You may need to name your index prior to exporting or if you find it easier just reset_index to restore the index back as a column and then call to_csv

Upvotes: 2

Related Questions