如何将每 2 行合并为 1 行
How to combine every 2 rows into 1 row
我正在尝试修复这个 NBA 比赛细节 table。
table中每两行是相同的游戏(GAME_ID)。我想让两行在一行,这样主队和客队的数据就会在同一行。
您可以使用 set_index 并展开,然后展平 headers。
这是我为棒球游戏编写的一些代码,我必须创建自己的游戏编号:
# source datafrome
year = '2020'
df = pd.read_excel('https://www.sportsbookreviewsonline.com/scoresoddsarchives/mlb/mlb%20odds%202011.xlsx')
df = df.dropna(how="all").copy()
df["Date"] = df["Date"].astype(int) # To handle excel float type in some years
df["Date"] = pd.to_datetime(
f"{year}" + df["Date"].astype(str).str.rjust(4, "0"), format="%Y%m%d"
)
cols = ["Date", "VH", "Team", "Final", "Close"]
df = df[cols]
df = df[df["VH"].isin(["V", "H"])]
# Flatten teams lines in to games
df["Game No"] = (df["VH"] == "V").cumsum()
df = df.set_index(["Game No", "Date", "VH"]).unstack()
df.columns = df.columns.map("_".join)
df = df.reset_index()
输出:
Game No Date Team_H Team_V Final_H Final_V Close_H Close_V
0 1 2020-03-31 WAS ATL 0 2 119 -139
1 2 2020-03-31 CIN MIL 7 6 -110 -110
2 3 2020-03-31 STL SDG 3 5 -178 158
3 4 2020-03-31 LOS SFO 2 1 -114 -106
4 5 2020-03-31 NYY DET 6 3 -152 132
... ... ... ... ... ... ... ... ...
2462 2463 2020-10-22 TEX STL 7 16 -183 163
2463 2464 2020-10-23 TEX STL 4 0 -171 151
2464 2465 2020-10-24 TEX STL 4 2 -130 110
2465 2466 2020-10-27 STL TEX 10 9 -114 -106
2466 2467 2020-10-28 STL TEX 6 2 -130 110
[2467 rows x 8 columns]
我正在尝试修复这个 NBA 比赛细节 table。 table中每两行是相同的游戏(GAME_ID)。我想让两行在一行,这样主队和客队的数据就会在同一行。
您可以使用 set_index 并展开,然后展平 headers。
这是我为棒球游戏编写的一些代码,我必须创建自己的游戏编号:
# source datafrome
year = '2020'
df = pd.read_excel('https://www.sportsbookreviewsonline.com/scoresoddsarchives/mlb/mlb%20odds%202011.xlsx')
df = df.dropna(how="all").copy()
df["Date"] = df["Date"].astype(int) # To handle excel float type in some years
df["Date"] = pd.to_datetime(
f"{year}" + df["Date"].astype(str).str.rjust(4, "0"), format="%Y%m%d"
)
cols = ["Date", "VH", "Team", "Final", "Close"]
df = df[cols]
df = df[df["VH"].isin(["V", "H"])]
# Flatten teams lines in to games
df["Game No"] = (df["VH"] == "V").cumsum()
df = df.set_index(["Game No", "Date", "VH"]).unstack()
df.columns = df.columns.map("_".join)
df = df.reset_index()
输出:
Game No Date Team_H Team_V Final_H Final_V Close_H Close_V
0 1 2020-03-31 WAS ATL 0 2 119 -139
1 2 2020-03-31 CIN MIL 7 6 -110 -110
2 3 2020-03-31 STL SDG 3 5 -178 158
3 4 2020-03-31 LOS SFO 2 1 -114 -106
4 5 2020-03-31 NYY DET 6 3 -152 132
... ... ... ... ... ... ... ... ...
2462 2463 2020-10-22 TEX STL 7 16 -183 163
2463 2464 2020-10-23 TEX STL 4 0 -171 151
2464 2465 2020-10-24 TEX STL 4 2 -130 110
2465 2466 2020-10-27 STL TEX 10 9 -114 -106
2466 2467 2020-10-28 STL TEX 6 2 -130 110
[2467 rows x 8 columns]