【问题标题】:Pandas scraped data not working in pandasPandas 抓取的数据在 pandas 中不起作用
【发布时间】:2018-05-19 10:09:54
【问题描述】:

为什么当我在 Excel 中手动输入数据时,pandas 可以工作。然而,当我抓取数据时,将其放入 csv 中。它给了我:

    zz = df1.WE=np.where(df3.AL.isin(df1.EW),df1.WE,np.nan)
ValueError: operands could not be broadcast together with shapes (148,) (537,) () 

其他网站没有发生这种情况。我在这里遗漏了一些明显的东西吗?是excel格式不正确还是这里的数据不一样?

df3

df3 = pd.DataFrame(columns=['DAT', 'G', 'TN', 'O1', 'L1', 'TN2', 'O2', 'L2', 'D', 'AJ', 'AK', 'AL'])

df1

                                         EW    WE  \
0             Ponte Preta U20 v Cruzeiro U20  2.10   
1  Fluminense RJ U20 v Defensor Sporting U20  2.00   
2              Gremio RS U20 v Palmeiras U20  3.30   
3                       Barcelona v Sporting  1.33   

                                                  DA  
0  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
1  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
2  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
3  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  

代码:

df3 = pd.DataFrame(columns=['DAT', 'G', 'TN', 'O1', 'L1', 'TN2', 'O2', 'L2', 'D', 'AJ', 'AK', 'AL'])

df3['DAT'] = df2['AA']



zz = df1.WE=np.where(df3.AL.isin(df1.EW),df1.WE,np.nan)
print(zz)

我已经提供了创建数据框 1、2 和 pandas 代码的所有脚本,直到它创建错误 here

我不断得到

错误:

    zz = df1.WE=np.where(df3.AL.isin(df1.EW),df1.WE,np.nan)
ValueError: operands could not be broadcast together with shapes (0,) (4,) ()

从抓取创建的错误文件并作为数据框加载:

如果这还不够,我也会按原样加载文件。

File 1, File 2

手动创建的工作文件:

工作:

File 1, File 2

知道如何解决这个问题吗?

【问题讨论】:

    标签: python python-3.x pandas selenium screen-scraping


    【解决方案1】:

    我认为你需要改变:

    df1.WE=np.where(df3.AL.isin(df1.EW),df1.WE,np.nan)
    

    df1.WE=np.where(df1.EW.isin(df2.AL),df1.WE,np.nan)
    

    问题是 DataFrame 与真实数据的长度不同。所以需要用另一个数据从df1 更改数据-comapring 返回与df1 相同长度的maks,并且没有错误。

    使用您的数据:

    df1 = pd.read_csv('df1.csv', names=['a','b','c'])
    print (df1.head())
                                               a     b  \
    0             Ponte Preta U20 v Cruzeiro U20  2.10   
    1  Fluminense RJ U20 v Defensor Sporting U20  2.00   
    2              Gremio RS U20 v Palmeiras U20  3.30   
    3                       Barcelona v Sporting  1.33   
    4                        Bayern Munich v PSG  2.40   
    
                                                       c  
    0  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    1  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    2  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    3  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    4  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    

    df2 = pd.read_csv('df2.csv', names=['a','b','c', 'd', 'e'])
    print (df2.head())
                     a                    b                  c     d  \
    0          In-Play      CSKA Moscow U19        Man Utd U19  1.14   
    1          In-Play  Atletico Madrid U19        Chelsea U19  1.01   
    2          In-Play         Juventus U19     Olympiakos U19  1.40   
    3  Starting in 22'       Paris St-G U19  Bayern Munich U19  2.24   
    4      Today 21:00         Man City U19       Shakhtar U19  2.66   
    
                                                       e  
    0  https://www.betfair.com.au/exchange/plus/footb...  
    1  https://www.betfair.com.au/exchange/plus/footb...  
    2  https://www.betfair.com.au/exchange/plus/footb...  
    3  https://www.betfair.com.au/exchange/plus/footb...  
    4  https://www.betfair.com.au/exchange/plus/footb...  
    

    比较数字列,这里是bd

    df1.b=np.where(df1.b.isin(df2.d),df1.b,np.nan)
    #first 5 values is NaNs
    print (df1.head())
                                               a   b  \
    0             Ponte Preta U20 v Cruzeiro U20 NaN   
    1  Fluminense RJ U20 v Defensor Sporting U20 NaN   
    2              Gremio RS U20 v Palmeiras U20 NaN   
    3                       Barcelona v Sporting NaN   
    4                        Bayern Munich v PSG NaN  
    

                                                       c  
    0  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    1  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    2  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    3  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    4  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    
    #check if some not NaNs values in b column
    print (df1[df1.b.notnull()])
                                           a      b  \
    23                Swindon v Forest Green   1.40   
    50       Sportivo Barracas v Canuelas FC  13.00   
    80                              FC Nitra   1.53   
    81                                   0-0   1.40   
    83       Cape Town City v Maritzburg Utd   1.53   
    84         Mamelodi Sundowns v Baroka FC   3.75   
    90  Dorking Wanderers v Tonbridge Angels   1.53   
    95             Coalville Town v Stamford   1.40   
    
                                                        c  
    23  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    50  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    80  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    81  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    83  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    84  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    90  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    95  https://www.bet365.com.au/#/AC/B1/C1/D13/E40/F...  
    

    您的测试数据的另一个问题是行数相同(4),所以没有错误。

    【讨论】:

    • 我得到: raise ValueError('Length of values does not match length of ''index') ValueError: Length of values does not match length of index for: df3['O2'] = zz @ 987654321@
    • 不知道你可以看看stackoverflow.com/questions/47726633/… 吗?尝试为 3 个数据帧创建 vlookup。
    【解决方案2】:

    顺便说一句,我建议将 pandas 函数与 pandas 一起使用:

    df1.loc[~df1.EW.isin(df2.AL), 'WE'] = np.nan
    

    【讨论】:

    • 我不建议写像x = y = 4+5 这样的东西,因为它很难阅读。这里显然我们没有为 d['O2'] 分配任何东西,因此,很可能,它都是空的。也许,这会奏效吗? df3.loc[~df3.EW.isin(df2.AL), 'WE'] = np.nan 然后df3['O3'] = df3.WE
    • 呃等等没关系。尽管试图调整它,但该代码对我不起作用
    • 所以对我来说,这会创建一个空的 O3 列
    【解决方案3】:

    好的,让我们回到绘图板上。上面的代码更简洁,但与您使用 numpy 所做的完全相同。让我们拆分您的代码。

    1) 我强烈建议您使用 jupyter / jupyter notebooks 来处理数据并了解每一行发生了什么。看看这里,例如: https://gist.github.com/Casyfill/f432966ebabd93f4271e27a1e2e76579

    所以,您的 df1 有 100 行和 3 列。您的 df2 有 42 行和 5 列。

    现在,您将 df3 创建为一个空数据框(0 行),但有 12 列(顺便说一下,也许您应该使用更多解释性的列名)。这一步完全没问题,而您不必事先定义所有列。

    让我们进入第二行: df3['DAT'] = df2['AA']

    这里基本上是从第二个数据框中复制列。现在,由于我们之前在 df3 中没有任何行,因此这是完全合法的操作。通过这样做,您将在 df3.xml 中创建 42 行。同样,这条线本身就很好。

    现在,最后一行。这里的逻辑如下:首先,对于 df3 中的每一行,我们检查df3.AL 的单元格(其值)是否在df1.EW 列中。请注意,我们之前从未定义过 df3.AL,因此整个列仅包含 NAN,因此这本身没有任何意义。

    接下来,我们假设 df3.AL 中有一些东西。当我们逐行检查所有内容时,我们将得到一个 pd.Series (想想 - 一列)作为这个测试的结果的布尔值,列有 42 行。现在,我们尝试将此列用作“掩码”,它定义 df1.WE 是否应该相同或默认为 Nan。但你不能这样做,因为 df1 有 100 行,而不是 42 行! Hense,我们有一个错误。

    因此,您需要重新定义您在这里实际想要做的事情 - 不清楚您在这里实际需要做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-07
      • 2019-06-22
      • 2021-02-28
      • 1970-01-01
      • 2020-09-16
      • 2019-05-16
      • 2017-02-15
      • 2019-11-14
      相关资源
      最近更新 更多