【问题标题】:How to convert list of strings into two column data frame base on the subsequent string in the list如何根据列表中的后续字符串将字符串列表转换为两列数据框
【发布时间】:2021-10-10 14:27:38
【问题描述】:

我已经能够从网站上抓取数据,但由于电子商务项目中的一些缺失值,数据不一致,我使用 beautifulsoup 获取数据, 下面是示例数据集,其中评级紧跟在产品名称之后,有些没有,后面跟着另一个产品名称,所以在这方面我想将它们设置为“无评级”。 这是我第一次作为python新手 提前致谢

data = ['Samsung Galaxy A12 ',
 '5 out of 5(6)',
 'Screenguard',
 'Samsung Galaxy Mos / A02s ',
 '4 out of 5(1)'
  'Pillow']

我实际上想将它转换为基于某些项目下的后续评级(5 分中的 5(6)个)的数据框,有些项目有评级,而有些没有,这就是它们在数据中不一致的原因。

下面的预期数据框

   product                          ratings
   Samsung Galaxy A12               5 out of 5(6)
   Screenguard                      No rating
   Samsung Mos / A02s               4 out of 5(1)
   Pillow                           No rating

【问题讨论】:

  • 在刮的时候为什么不这样做呢?
  • “out of”会一直用于评分吗?
  • 不,这就是问题所在,未评级的产品并不总是存在“out of”,它甚至什么都没有,网站上总是空白
  • @norie 数据来自不同的类,所以我的计划是分别获取它们,将它们附加到列表中并将所有结果合并到一个数据框中,但是当我这样做时,结果不会'由于缺少其他产品的评分而导致押韵,这就是为什么我不得不使用另一种方法来获得我在这里作为示例发送的结果

标签: python pandas string dataframe beautifulsoup


【解决方案1】:

这适用于发布的数据,但我建议您调整用于抓取的代码,以便在找不到评级时返回“无评级”。

import pandas as pd

data = [
    "Samsung Galaxy A12 ",
    "5 out of 5(6)",
    "Screenguard",
    "Samsung Galaxy Mos / A02s ",
    "4 out of 5(1)",
    "Pillow",
]

products = [product for product in data if not "out of" in (product)]

ratings = []

idx = 1

for product in products:
    idx = data.index(product)

    if idx>=len(data)-1:
        ratings.append('No rating')
    elif  not 'out of' in data[idx+1]:
        ratings.append('No rating')
    else:
        ratings.append(data[idx+1])

df = pd.DataFrame({'product':products, 'rating': ratings})
Sample Output

                      product         rating
0         Samsung Galaxy A12   5 out of 5(6)
1                 Screenguard      No rating
2  Samsung Galaxy Mos / A02s   4 out of 5(1)
3                      Pillow      No rating

【讨论】:

  • 哇,非常感谢,你让我摆脱了压力
【解决方案2】:

正如 cmets 中提到的,我认为在 scraping 时处理这个问题会更好/更干净。

这是我认为可以解决您的问题的代码。


#Init data
import pandas as pd

data = ['Samsung Galaxy A12 ',
 '5 out of 5(6)',
 'Screenguard',
 'Samsung Galaxy Mos / A02s ',
 '4 out of 5(1)'
  'Pillow']

# Create function
def clean_data_to_df(data):
    phones, ratings = [], []
    for idx, value in enumerate(data):
        # 1st phone
        if idx == 0:
            phones.append(value)
            continue
        # Add rating
        if 'out of' in value:
            ratings.append(value)
            continue

        # If not a rating, it is a phone.
        phones.append(value)

        if 'out of' not in data[idx-1]:
            ratings.append('No Rating')

    if len(phones)>len(ratings):
        ratings.append('No Rating')
    return pd.DataFrame({'phone':phones, 'ratings':ratings})

clean_data_to_df(data)

输出



        phone                   ratings
0   Samsung Galaxy A12          5 out of 5(6)
1   Screenguard                 No Rating
2   Samsung Galaxy Mos / A02s   4 out of 5(1)
3   Pillow                      No Rating

【讨论】:

  • 感谢您的贡献
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2019-04-28
  • 2020-09-21
  • 2019-09-14
  • 2017-12-19
  • 2022-12-11
  • 1970-01-01
相关资源
最近更新 更多