My current df looks like below for the column product_pair:
product_pair
---------------------
('B00XA1075Y', 'B002HQCWYM')
('B01G4I8WCE', 'B00H2AAXMQ')
...
I want to create two different columns for the above tuples like below:
product_pair | p1 | p2
----------- ------- ----------
('B00XA1075Y', 'B002HQCWYM') | B00XA1075Y | B002HQCWYM
('B01G4I8WCE', 'B00H2AAXMQ') | B01G4I8WCE | B00H2AAXMQ
...
How can I do this?
>Solution :
Try this:
import ast
df = pd.concat([df, pd.DataFrame(df['product_pair'].astype(str).apply(ast.literal_eval).tolist(), columns=['p1', 'p2'])], axis=1)
Output:
>>> df
product_pair p1 p2
0 (B00XA1075Y, B002HQCWYM) B00XA1075Y B002HQCWYM
1 (B01G4I8WCE, B00H2AAXMQ) B01G4I8WCE B00H2AAXMQ