Pandas 抓取的数据在 pandas 中不起作用

Pandas scraped data not working in pandas

为什么当我手动将数据输入 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

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

我认为你需要改变:

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 return maks 具有与 df1 相同的长度并且没有错误。

你的数据:

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...  

comapre 数字列,这里 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),所以没有错误。

附带说明一下,我建议使用 pandas 函数和 pandas:

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

好的,让我们回到绘图板。上面的代码更清晰,但与您使用 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 中创建了 42 行。同样,这一行本身很好。

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

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

所以你需要重新定义你在这里真正想做什么 - 目前还不清楚你在这里真正需要做什么。