Reputation: 65
I am trying to re-create a VBA macro that I have using Python. Could someone please tell me the FOR statement I should use, in order to get below result? Thank you very much.
file 1:
Product Colour Price
Book NaN 5
Table NaN 10
Chair NaN 7
file 2:
Colour
Blue
Red
Green
Expected result (file 1) after the loop:
Product Colour Price
Book Blue 5
Table Blue 10
Chair Blue 7
Book Red 5
Table Red 10
Chair Red 7
Book Green 5
Table Green 10
Chair Green 7
Upvotes: 0
Views: 72
Reputation: 164623
You can use a list comprehension, analogous to a nested for
loop:
df = pd.DataFrame([[product, colour, price] for colour in df2['Colour'] \
for product, price in zip(df1['Product'], df1['Price'])],
columns=['Product', 'Colour', 'Price'])
print(df)
Product Colour Price
0 Book Blue 5
1 Table Blue 10
2 Chair Blue 7
3 Book Red 5
4 Table Red 10
5 Chair Red 7
6 Book Green 5
7 Table Green 10
8 Chair Green 7
Upvotes: 0
Reputation: 13255
First duplicate values of df1
by length of df2
and then use list comprehesion
and chain
for Colour
as:
from itertools import chain
df = pd.DataFrame({'Product': df1['Product'].values.tolist()*len(df2),
'Price' : df1['Price'].values.tolist()*len(df2),
'Colour' : list(chain.from_iterable([[v]*len(df1) for v in df2['Colour'].values.tolist()]))})
print(df)
Product Price Colour
0 Book 5 Blue
1 Table 10 Blue
2 Chair 7 Blue
3 Book 5 Red
4 Table 10 Red
5 Chair 7 Red
6 Book 5 Green
7 Table 10 Green
8 Chair 7 Green
Upvotes: 1