1

I have a pandas DataFrame, and one column’s values that I want to use are lists. I want to combine two elements one by one of each list, and output into another DataFrame.
For example, I have dataframe df, which contains col_a and col_b. The values of col_b are lists. I want to loop values of df.col_b, output a paired lists.

import pandas as pd

df=pd.DataFrame({'col_a':['ast1','ast2','ast3'],'col_b':[['text1','text2','text3'],['mext1','mext2','mext3'],['cext1','cext2']]})
df

    col_a   col_b
0   ast1    [text1, text2, text3]
1   ast2    [mext1, mext2, mext3]
2   ast3    [cext1, cext2]

I want this:

    col_a   col_b_1
0   ast1    [text1, text2]
1   ast1    [text1, text3]
2   ast1    [text2, text3]
3   ast2    [mext1, mext2]
4   ast2    [mext1, mext3]
5   ast2    [mext2, mext3]
6   ast3    [cext1, cext2]

2 Answers 2

1

Assuming your col_a has unique value per row, you can use combinations from itertools to generate all two combinations of the list element:

from itertools import combinations
(df.groupby('col_a')['col_b']
   .apply(lambda x: pd.Series(list(combinations(x.iloc[0], 2))))
   .reset_index(level = 0))

#  col_a            col_b
#0  ast1    (text1, text2)
#1  ast1    (text1, text3)
#2  ast1    (text2, text3)
#0  ast2    (mext1, mext2)
#1  ast2    (mext1, mext3)
#2  ast2    (mext2, mext3)
#0  ast3    (cext1, cext2)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use itertools to flatten the lists:

import itertools
series = df["col_b"].apply(lambda x: \
    pd.Series(list(itertools.combinations(x,2)))).stack()

The series must have a name to be mergeable with the "mother" dataframe:

series.name = "col_b_1"

Now, merge the two data objects and select the columns that you want:

result = df.merge(pd.DataFrame(series).reset_index(),
    left_index=True,
    right_on="level_0")[["col_a","col_b_1"]]

The result is a column of tuples; if this is not ok, .apply() function list() to it.

#   col_a         col_b_1
# 0  ast1  (text1, text2)
# 1  ast1  (text1, text3)
# 2  ast1  (text2, text3)
# 3  ast2  (mext1, mext2)
# 4  ast2  (mext1, mext3)
# 5  ast2  (mext2, mext3)
# 6  ast3  (cext1, cext2)

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.