簡體   English   中英

嘗試在循環內附加兩個Pandas DataFrame,導致第一個被覆蓋

[英]Attempting to append two Pandas DataFrames within a loop causes the first to be overwritten

我有這個功能(除其他外)應該讀取棒球比賽的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

在每個循環中,您都在重新分配變量noseclean_h

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

然后,在每個循環上, nosecleaned_h = nosecleaned_h.append(nosecleaned_h, ignore_index=False)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM