尝试在循环中附加两个数据帧会导致覆盖第一个数据帧

2024-05-29 05:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个函数(除其他外)应该读取棒球比赛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

Tags: 数据indfhomefordateindexrr
1条回答
网友
1楼 · 发布于 2024-05-29 05:25:20

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

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

然后,在每个循环上nosecleaned_h = nosecleaned_h.append(nosecleaned_h, ignore_index=False)被替换。你知道吗

相关问题 更多 >

    热门问题