0

This question is related to the following thread:

How would you zip an unknown number of lists in Python?

I'm working with a dataframe (merged_data) that has two fields ("campaignName" and "creativeName") where I'd like to produce a list of every pair-wise combination of values within the two fields. Based on the guidance above, here's the code that I thought would do it:

print varlist print merged_data[varlist] combo = zip(*merged_data[varlist]) print combo combo2 = zip(merged_data[varlist[0]], merged_data[varlist[1]]) print combo2 

But here are the results of the two zip commands (which I had thought would produce an identical list of tuples):

['campaignName', 'creativeName']

 campaignName creativeName 0 MMedia Whatsapp 12-24-14 Pic1-Angle1-300x250 1 MMedia Whatsapp 12-24-14 Pic1-Angle1-300x250 ... 89 MMedia Whatsapp 12-25-14 Pic4-Angle1-300x250 [90 rows x 2 columns] [('c', 'c'), ('a', 'r'), ('m', 'e'), ('p', 'a'), ('a', 't'), ('i', 'i'), ('g', 'v'), ('n', 'e'), ('N', 'N'), ('a', 'a'), ('m', 'm'), ('e', 'e')] [('MMedia Whatsapp 12-24-14', 'Pic1-Angle1-300x250'), ('MMedia Whatsapp 12-24-14', 'Pic1-Angle1-300x250'), ('MMedia Whatsapp 12-24-14', 'Pic1-Angle1-300x250'), ..., ('MMedia Whatsapp 12-25-14', 'Pic4-Angle1-300x250')] 

Any idea what's going on? Obviously, in the first case it's treating ['campaignName', 'creativeName'] like a string of letters and in the case of the second, it's treating them like fields in the data frame and then looking at the values inside.

Appreciate any and all assistance here. Thanks in advance!

1 Answer 1

0

The two statements aren't actually the same:

combo1=zip(*combo = zip(*merged_data[varlist])

is equivalent to:

combo1=zip(merged_data[varlist][0],merged_data[varlist][1],merged_data[varlist][2]....)

and not

combo1=zip(merged_data[varlist[0]],merged_data[varlist[1]])

(note the position of the square brackets)

Strings are just sequences to Python so it unpacks them for you

I tend to think of zip(*args) as being the inverse of zip:

list1=[1,2,3,4,5] list2=[10,11,12,13,14] zippedlist=zip(list1,list2) print zippedlist >>>>[(1,10),(2,11),(3,12),(4,13),(5,14)] print zip(*zippedlist) >>>>[(1,2,3,4,5),(10,11,12,13,14)] 

EDIT:

An open ended version of your combo2 function can be implemented thus:

combo2=zip([merged_data[v] for v in varlist]) 
Sign up to request clarification or add additional context in comments.

2 Comments

OK, makes sense. Really appreciate the clarification but I probably didn't ask the question. What I'm ultimately trying to do is replicate this: combo2 = zip(merged_data[varlist[0]], merged_data[varlist[1]]) But also making it flexible enough that varlist can have one variables, two variables, three variables, etc. It sounds like zip(*args) might not be the way to go here. Is there a better alternative? Thanks again!
Oh right, I understand. Yes it can be done, have edited answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.