Pandas write variable number of newlines from list in series

I am using Pandas

as a way to write data from Selenium

.

Two examples are output from a search box ac_results

on a web page:

#Search for product_id = "01"
ac_results = "Orange (10)"

#Search for product_id = "02"
ac_result = ["Banana (10)", "Banana (20)", "Banana (30)"]

      

Orange only returns one price ($ 10), while Banana returns a variable amount of prices from different vendors, in this example three prices ($ 10), ($ 20), ($ 30).

The code uses regex through re.findall

to grab each price and put them in a list. The code works fine as long as it re.findall

finds only one element of the list, as for oranges. The problem is that there is a variable number of prices, as when searching for Bananas. I would like to create a new line for each quoted price, and the lines must also include product_id

and item_name

.

Current output:

product_id      prices                  item_name
01              10                      Orange
02              [u'10', u'20', u'30']   Banana

      

Desired output:

product_id      prices                  item_name
01              10                      Orange
02              10                      Banana
02              20                      Banana
02              30                      Banana

      

Current code:

df = pd.read_csv("product_id.csv")
def crawl(product_id):
    #Enter search input here, omitted
    #Getting results:
    search_result = driver.find_element_by_class_name("ac_results")
    item_name = re.match("^.*(?=(\())", search_result.text).group().encode("utf-8")
    prices = re.findall("((?<=\()[0-9]*)", search_reply.text)
    return pd.Series([prices, item_name])

df[["prices", "item_name"]] = df["product_id"].apply(crawl)
df.to_csv("write.csv", index=False)

      

FYI: Workable solution with module csv

, but I want to use Pandas

.

with open("write.csv", "a") as data_write:
    wr_data = csv.writer(data_write, delimiter = ",")
    for price in prices: #<-- This is the important part!
        wr_insref.writerow([product_id, price, item_name])

      

+3


source to share


2 answers


# initializing here for reproducibility
pids = ['01','02']
prices = [10, [u'10', u'20', u'30']]
names = ['Orange','Banana']
df = pd.DataFrame({"product_id": pids, "prices": prices, "item_name": names})

      

The following snippet should work after yours apply(crawl)

.



# convert all of the prices to lists (even if they only have one element)
df.prices = df.prices.apply(lambda x: x if isinstance(x, list) else [x])

# Create a new dataframe which splits the lists into separate columns.
# Then flatten using stack. The explicit MultiIndex allows us to keep
# the item_name and product_id associated with each price.
idx = pd.MultiIndex.from_tuples(zip(*[df['item_name'],df['product_id']]), 
                                names = ['item_name', 'product_id'])
df2 = pd.DataFrame(df.prices.tolist(), index=idx).stack()

# drop the hierarchical index and select columns of interest
df2 = df2.reset_index()[['product_id', 0, 'item_name']]
# rename back to prices
df2.columns = ['product_id', 'prices', 'item_name']

      

+3


source


I was unable to run your code (possibly missing inputs), but you can probably convert your list prices

to a dict list and then build in there DataFrame

:

 d = [{"price":10, "product_id":2, "item_name":"banana"}, 
      {"price":20, "product_id":2, "item_name":"banana"}, 
      {"price":10, "product_id":1, "item_name":"orange"}]
df = pd.DataFrame(d)

      



Then df

:

  item_name  price  product_id
0    banana     10           2
1    banana     20           2
2    orange     10           1

      

0


source







All Articles