2

I have an Excel document with following inputs:

Row 1: Name of the product- 7up soft drink
Row 2: Diffrent varients- 600ml;1.25lt;2.25lt;600ml-lemon;2.25lt-lemon
Row 3: price of its Vareints- 35;60;90;35;90

I want the output to look like this

7up soft dring 600 ml        35
7up soft drink 1.25ltr       60
7up soft drink 2.25ltr       90
7up soft drink 600ml lemon   35
7up soft drink 2.25ltr lemon 90

I have come this far using python

import os
import pandas as pd


os.chdir('/Users/apple/Desktop')

tab1 = pd.read_excel('master.xlsx')
tab1['measure'] = tab1['measure'].apply(lambda x: str(x))
tab1['measure'] = tab1['measure'].apply(lambda x: x.split(';'))
tab1['price'] = tab1['price'].apply(lambda x: str(x))
tab1['price'] = tab1['price'].apply(lambda x: x.split(';'))


test = tab1.iloc[13]

tdc = dict.fromkeys(test.index)
for i in tdc.keys():
    tdc[i] = []

tls = []
for i, j in zip(test['measure'],test['price']):
    tls.append([test['products'] + ' ' + i, i, int(j)])

for x in tls:
    for i, j in zip(['products', 'measure', 'price'], x):
        tdc[i].append(j)

I got this output

Measure list
['600ml', '1.25lt', '2.25lt', '600ml-lemon', '2.25lt-lemon']
Price list
[35, 60, 90, 35, 90]
Products list
['7up soft dring 600 ml',
'7up soft drink 1.25ltr',
'7up soft drink 2.25ltr',
'7up soft drink 600ml lemon',
'7up soft drink 2.25ltr lemon']

How to assign price list to product list?

QQAA
  • 47
  • 9

2 Answers2

2

Since the price list and product list are synchronized (the first product has the first price, the second product as the second price, etc.), you can use a simple for loop:

for i in range(len(product_list)):
    print('{}\t{}'.format(product_list[i], price_list[i]))

If you want to create a new list:

output = []
for i in range(len(product_list)):
    output.append('{} {}'.format(product_list[i], price_list[i]))
brandonwang
  • 1,603
  • 10
  • 17
1

You need to combine data from two lists. A way to do this is to use zip or itertools.izip.

See e.g. Zip lists in Python

A one-liner that produces your desired output:

print('\n'.join(['{:29}{}'.format(product, price) for product, price in zip(test['products'], test['price'])]))

Example:

>>> test = {
...     'products': [
...         '7up soft dring 600 ml',
...         '7up soft drink 1.25ltr',
...         '7up soft drink 2.25ltr',
...         '7up soft drink 600ml lemon',
...         '7up soft drink 2.25ltr lemon',
...     ],
...     'price': [
...         35,
...         60,
...         90,
...         35,
...         90,
...     ]
... }

# Get the required textual output:
>>> print('\n'.join(['{:29}{}'.format(product, price) for product, price in zip(test['products'], test['price'])]))
7up soft dring 600 ml        35
7up soft drink 1.25ltr       60
7up soft drink 2.25ltr       90
7up soft drink 600ml lemon   35
7up soft drink 2.25ltr lemon 90

# Get a list with (product, price) tuples
>>> zip(test['products'], test['price'])
[('7up soft dring 600 ml', 35), ('7up soft drink 1.25ltr', 60), ('7up soft drink 2.25ltr', 90), ('7up soft drink 600ml lemon', 35), ('7up soft drink 2.25ltr lemon', 90)]
bohrax
  • 1,051
  • 8
  • 20