1

I'm new to pandas and trying to figure out how to add two different variables values in the same column.

import pandas as pd
import requests
from bs4 import BeautifulSoup
itemproducts = pd.DataFrame()

url = 'https://www.trwaftermarket.com/en/catalogue/product/BCH720/'
r = requests.get(url)
soup = BeautifulSoup(r.content,'lxml')

code_name = soup.find_all('div',{'class':'col-sm-6 intro-section reset-margin'})
for head in code_name:
    item_code  = head.find('span',{'class':'heading'}).text
    item_name  = head.find('span',{'class':'subheading'}).text

for tab_ in tab_4:
    ab = tab_.find_all('td')
    make_name1    = ab[0].text.replace('Make','')
    code1         = ab[1].text.replace('OE Number','')
    make_name2    = ab[2].text.replace('Make','')
    code2         = ab[3].text.replace('OE Number','')

itemproducts=itemproducts.append({'CODE':item_code,
                                'NAME':item_name,
                                'MAKE':[make_name1,make_name2],
                                'OE NUMBER':[code1,code2]},ignore_index=True)

OUTPUT (Excel image)

enter image description here

What actually I want excel image

Mr. T
  • 11,960
  • 10
  • 32
  • 54

3 Answers3

0

In pandas you must specify all the data in the same length. So, in this case, I suggest that you specify each column or row as a fixed length list. For those that have one member less, append a NaN to match.

Reza Ghari
  • 52
  • 7
0

I couldn't reproduce the results from your script. However, based on your end dataframe, perhpas you can make use of explode together with apply the dataframe in the end:

#creating your dataframe
itemproducts = pd.DataFrame({'CODE':'BCH720','MAKE':[['HONDA','HONDA']],'NAME':['Brake Caliper'],'OE NUMBER':[['43019-SAA-J51','43019-SAA-J50']]})

>>> itemproducts
      CODE                MAKE           NAME                           OE NUMBER
0  BCH720  ['HONDA', 'HONDA']  Brake Caliper  ['43019-SAA-J51', '43019-SAA-J50']

#using apply method with explode on 'MAKE' and 'OE NUMBER'
>>> itemproducts.apply(lambda x: x.explode() if x.name in ['MAKE', 'OE NUMBER'] else x)
     CODE   MAKE           NAME      OE NUMBER
0  BCH720  HONDA  Brake Caliper  43019-SAA-J51
0  BCH720  HONDA  Brake Caliper  43019-SAA-J50
Grayrigel
  • 3,474
  • 5
  • 14
  • 32
  • Added an answer. Let me know if it works for you. If it does please consider accepting/check-marking the answer. – Grayrigel Oct 14 '20 at 12:45
0

I found a similar question here on stackoverflow that can help you. Another approach is to use explode function from Pandas Dataframe.

Below I put an example from pandas documentation.

>>> df = pd.DataFrame({'A': [[1, 2, 3], 'foo', [], [3, 4]], 'B': 1})
>>> df
           A  B
0  [1, 2, 3]  1
1        foo  1
2         []  1
3     [3, 4]  1

>>> df.explode('A')
     A  B
0    1  1
0    2  1
0    3  1
1  foo  1
2  NaN  1
3    3  1
3    4  1
Dharman
  • 30,962
  • 25
  • 85
  • 135
Carlos Henrique
  • 177
  • 2
  • 8