Getting a tuple in a Dafaframe into multiple rows
<p>I have a Dataframe, which has two columns (Customer, Transactions).
The  Transactions column is a tuple of all the transaction id's of that customer.</p>

<pre><code>Customer Transactions
1        (a,b,c)
2        (d,e)
</code></pre>

<p>I want to convert this into a dataframe, which has customer and transaction id's, like this.</p>

<pre><code>Customer  Transactions
1         a
1         b
1         c
2         d
2         e
</code></pre>

<p>We can do it using loops,  but is there a straight 1 or 2 lines way for doing that.</p>

<p>I think following is faster:</p>

<pre><code>import numpy as np
import random
import string
import pandas as pd
from itertools import chain

customer = np.unique(np.random.randint(0, 1000000, 100000))
transactions = [tuple(string.ascii_letters[:random.randint(3, 10)]) for _ in range(len(customer))]
df = pd.DataFrame({"customer":customer, "transactions":transactions})

df2 = pd.DataFrame({
        "customer": np.repeat(df.customer.values, df.transactions.str.len()),
        "transactions": list(chain.from_iterable(df.transactions))})
</code></pre>

python pandas dataframe tuples reshape