简体   繁体   中英

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

I have this function that (among other things) is supposed to read a baseball matches csv file create a list of all the teams (this part works). The file has away match data and home match data, the idea is to split the data change the columns and lastly append the matches data regardless of location (this last part does not work). Instead of appending my away and home games into one dataframe the code overwrites the home games entirely.

I have attached my code along with the question.

Thank you so much for all your help.

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

On each loop, you are reassigning the variable noseclean_h :

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

Then, on each loop the nosecleaned_h = nosecleaned_h.append(nosecleaned_h, ignore_index=False) is replaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM