1

I'm analysing some data with Apriori algorithm. This requires me to convert the dataframe into an array of tuples( groupby col"c1"), with each tuple corresponding to a "row" of the dataframe. If there's only one value in the tuple, I want an "," behind it cuz I need to keep it as a tuple.

In [1]: data
Out[1]:
   c1   c2
0  r1   aa
1  r1   bb
2  r1   cc
3  r2   dd
4  r2   ee
5  r3   ff

I expect the data like this:

[('aa','bb','cc'),('dd','ee'),('ff',)]

1 Answer 1

3

Use GroupBy.apply with tuple and convert Series to list:

a = df.groupby('c1')['c2'].apply(tuple).tolist()
print (a)
[('aa', 'bb', 'cc'), ('dd', 'ee'), ('ff',)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.