Ad
Remove Duplicate Values In A Tuple Array In Python
I have a party whom purchases products. Every time the customer purchases a product, a new row is generated with the same party number.
I have grouped the products on party number and I am now stuck with a column which has arrays of tuples in it
Party Nbr | Product |
---|---|
1 | (a, a, a, a, b, c) |
2 | (a, d, a, a) |
3 | (a, a, b, b, b) |
I cant find how I can remove all duplicates from each row of the product column.
Code for the groupby:
pf = prod.groupby(['Party Nbr'])['Product name'].apply(tuple).reset_index().rename(columns= {'Product name': 'Product'})
pf['Product'] = tuple(set(pf['Product']))
ValueError: Length of values (4663) does not match length of index (32539)
Someone able to help me?
Ad
Answer
Assuming, you are using pandas
, I recreated your table into a dataframe, and show how you could do the transform.
In [11]: df = pd.DataFrame({
"party": [1, 2, 3],
"product": [
("a", "a", "a", "a", "b", "c"),
("a", "d", "a", "a"),
("a", "a", "b", "b", "b")]})
In [12]: df
Out[12]:
party product
0 1 (a, a, a, a, b, c)
1 2 (a, d, a, a)
2 3 (a, a, b, b, b)
In [13]: df["product"] = df["product"].apply(set).apply(tuple)
In [14]: df
Out[17]:
party product
0 1 (c, b, a)
1 2 (a, d)
2 3 (b, a)
Note: as mentioned in the comments, the order of the products is not preserved, you want to preserve the order, you can use a custom function in place of chaining set
& tuple
.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad