为什么在执行df2.groupby(df2.index).sum()时会删除列

时间:2019-05-26 20:34:16

标签: pandas-groupby

我正在尝试为相似的索引值添加列值,但是列将被删除。

import numpy as np
import pandas as pd
df2=pd.DataFrame()
inh=[7597,35933,28081]
for id in inh:
    player_url=requests.get(f'http://stats.espncricinfo.com/ci/engine/player/{id}.html?class=2;template=results;type=batting').text
    sp = BeautifulSoup(player_url, 'lxml')
    for ls in sp.select("tr.data1>td.left"):
        if re.search("year",ls.text):
            year=str(ls.text).split("year")[1]
            runs=ls.find_next_siblings("td")[4]
            df1=pd.DataFrame([runs.text],[id],[year])
            df2=df2.append(df1)


df2.head()

预期输出:

       1972 1974    2004    2005    2006    2007    2008    2009    2010
382      50   0        0       0       0       0         0     0       0
222       0       44       0       0       0       0         0     0       0
3542      0       0        43      56      32      23       152   233      0

但是我得到了:

    1972    1974
7597    50  0
28081   0   0
35933   0   67

0 个答案:

没有答案