Reputation:
What I'm trying to do is optimize my current code that uses dataframes. The dataframes follow a similar naming convention, so instead of explicitly naming them, I'm trying to create them on the fly and then process them.
days = [1, 2, 3, 4, 5]
#1,2,3,4,5 are dataframes
endings = ['_operation1', '_operation2']
suffixedFrames = [x.add_suffix(y) for x, y in zip(days, endings)]
print(suffixedFrames)
The problem is that when I run this, it only prints out two dataframes, specifically, 1_operation1 and 1_operation2. How do I make it add the suffix to every dataframe in list days?
Upvotes: 2
Views: 555
Reputation: 819
Zip does not work as you want to, but it creates an iterator of tuples, where the i-th tuple contains the i-th element of the 2 list you're using, as you can see in the documentation. In your case if you do print(list(zip(days, endings)))
you will see this:
[(1, '_operation1'), (2, '_operation2')]
.
In order to achieve what you want you can do as follows:
import itertools
days = [1, 2, 3, 4, 5]
#1,2,3,4,5 are dataframes
endings = ['_operation1', '_operation2']
suffixedFrames = [x.add_suffix(y) for x, y in list(itertools.product(days, endings))]
print(suffixedFrames)
Upvotes: 1