【问题标题】:Attempting to append two Pandas DataFrames within a loop causes the first to be overwritten尝试在循环中附加两个 Pandas DataFrame 会导致第一个被覆盖
【发布时间】:2017-12-07 06:51:33
【问题描述】:

我有这个功能(除其他外)应该读取棒球比赛 csv 文件创建所有球队的列表(这部分有效)。该文件具有客场比赛数据和主场比赛数据,其想法是拆分数据更改列并最后附加匹配数据,而不管位置如何(最后一部分不起作用)。代码没有将我的客场和主场比赛附加到一个数据帧中,而是完全覆盖了主场比赛。

我已附上我的代码以及问题。

非常感谢您的帮助。

df = pd.read_csv('C:\\Users\\data.csv', index_col=0)

unique = df['Team Home'].unique()
inplace = ['H', 'A']
myway = pd.DataFrame()
for i in range(len(unique)):
    for inp in inplace:
        if inp == 'H':    #loop to find column names with 'Home' and 'Away' Labels
            located = 'Home'
            character = 'H'
        else:
            located = 'Away'
            character = 'A'
        noseclean_h = df[df['Team {}'.format(located)].isin([unique[i]])]
        noseclean_h = noseclean_h.sort_values('Date')
        home = [rr for rr in rolling_haiting if character in rr]
        new_home = [rr.replace('{}'.format(located), '').strip() if character in rr and len(rr) > 2
                    else rr.replace(character, '') for rr in home]
        new_home.append('Date')
        new_home.append('Team')
        home.append('Date')
        home.append('Team {}'.format(located))
        ncleaned = ncleaned[home]
        d = dict(zip(home, new_home))
        ncleaned .rename(columns=d, inplace=True)
        nosecleaned_h['Date'] = pd.to_datetime(ncleaned ['Date'])
        nosecleaned_h.set_index('Date', inplace=True) # set index to date to prevent overlapping
        nosecleaned_h = nosecleaned_h.append(nosecleaned_h, ignore_index=False)
    print(nosecleaned_h)
....etc

【问题讨论】:

    标签: python pandas dataframe append overlap


    【解决方案1】:

    在每个循环中,您都在重新分配变量noseclean_h

    noseclean_h = df[df['Team {}'.format(located)].isin([unique[i]])]
    

    然后,在每个循环中,nosecleaned_h = nosecleaned_h.append(nosecleaned_h, ignore_index=False) 被替换。

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 2020-06-09
      • 1970-01-01
      • 2018-03-22
      • 2016-10-28
      • 2021-02-24
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多