Home > Software design >  How to create two different columns from a fixed size tuple in Pandas
How to create two different columns from a fixed size tuple in Pandas

Time:03-13

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?

CodePudding user response:

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

CodePudding user response:

you could use the map/apply to iterate over the tuple.

df['p1'] = df['product_pair'].map(lambda x: x[0])
df['p2'] = df['product_pair'].map(lambda x: x[1])

CodePudding user response:

you can use when different shape tuple by same code.

df1 = pd.DataFrame(df["product_pair"].to_list())
df1.columns = df1.columns   1
pd.concat([df, df1.add_prefix("p")], axis=1)
  • Related