【问题标题】:I have 3 dataframes inside just one. How can I concat them? [duplicate]我只有一个里面有 3 个数据框。我怎样才能连接它们? [复制]
【发布时间】:2021-08-22 20:12:22
【问题描述】:

我真的沉迷于此。我不知道如何将这 3 个数据帧合二为一,因为它们位于一个数组左右。我真的需要你的帮助。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

str = 'https://fbref.com/en/comps/Big5/{}/{}-Big-5-European-Leagues-Stats'

seasons = ["2017-2018", "2018-2019", "2019-2020"]

for season in seasons:
    url = str.format(season, season)
    league = pd.read_html(url)
    league = league[0]
    league["Season"] = season
    print(type(league))


<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    你可以使用pandas.concat:

    import pandas as pd
    
    url = "https://fbref.com/en/comps/Big5/{}/{}-Big-5-European-Leagues-Stats"
    
    seasons = ["2017-2018", "2018-2019", "2019-2020"]
    
    dfs = []
    for season in seasons:
        league = pd.read_html(url.format(season, season))[0]
        dfs.append(league)
    
    df = pd.concat(dfs)
    print(df)
    
    df.to_csv("data.csv", index=False)
    

    打印:

        Rk            Squad  Country  LgRk  MP   W   D   L   GF  GA  GD  Pts  Pts/G    xG   xGA   xGD  xGD/90  Attendance                              Top Team Scorer                         Goalkeeper
    0    1  Manchester City  eng ENG     1  38  32   4   2  106  27  79  100   2.63  80.1  23.0  57.1    1.50       54070                           Sergio Agüero - 21                            Ederson
    1    2         Juventus   it ITA     1  38  30   5   3   86  24  62   95   2.50  59.8  28.7  31.0    0.82       39316                            Paulo Dybala - 22                   Gianluigi Buffon
    2    3    Bayern Munich   de GER     1  34  27   3   4   92  28  64   84   2.47  77.7  33.6  44.1    1.30       75000                      Robert Lewandowski - 29                       Sven Ulreich
    3    4        Paris S-G   fr FRA     1  38  29   6   3  108  29  79   93   2.45  89.2  32.2  57.0    1.50       46929                          Edinson Cavani - 28                    Alphonse Areola
    4    5        Barcelona   es ESP     1  38  28   9   1   99  29  70   93   2.45  78.3  41.1  37.2    0.98       66603                            Lionel Messi - 34              Marc-André ter Stegen
    
    ...
    

    并保存data.csv(来自 LibreOffice 的屏幕截图):

    【讨论】:

    • 祝你万岁哈哈
    【解决方案2】:

    看看这个... 希望这能解决您的问题。

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    str = 'https://fbref.com/en/comps/Big5/{}/{}-Big-5-European-Leagues-Stats'
    
    seasons = ["2017-2018", "2018-2019", "2019-2020"]
    
    dataframes = []
    
    for season in seasons:
        url = str.format(season, season)
        league = pd.read_html(url)
        league = league[0]
        league["Season"] = season
    
    
    
        # Changes
        dataframes.append(league)
    
    
    # Changes
    new_dataframe = pd.concat(dataframes)
    
    print(new_dataframe)
    

    我使用 panda 的 .concat() 方法连接列表中的所有数据帧。

    If you look at the season column, you can see the seasons 2017-2018, 2018-2019, and 2019-2020:

    希望这对您有所帮助。如果您还有其他问题,请随时提出。

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 2019-11-12
      • 2022-07-06
      相关资源
      最近更新 更多