gpt4 book ai didi

python - 按数字组合 Pandas 数据帧

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:09 28 4
gpt4 key购买 nike

我有 3 个名为 df1 的 Pandas 数据框, df2df3 .

df1:
match_up result
0 1985_1116_1234 1
1 1985_1120_1345 1
2 1985_1207_1250 1
3 1985_1229_1425 1
4 1985_1242_1325 1
5 1986_1116_1430 0
6 1986_1250_ 1229 0
7 1986_1207_1437 1

df2:
team_df2 win_df2
1207 0.700
1116 0.636
1120 0.621
1229 0.615
1242 0.679
1116 0.742
1207 0.567
1250 0.342

df3:
team_df3 win_df3
1234 0.667
1250 0.759
1325 0.774
1345 0.742
1425 0.667
1229 0.845
1430 0.434
1437 0.123

专栏team_df2在数据框中 df2是变量 year_ 之后的值(1985_) 在数据框中 df1 .专栏team_df3是变量year_val1_之后的值(1985_1116_])

df2 中的前 5 行和 df3代表 1985 年和数据框中的最后 3 行 df2df3代表 1986 年。

我需要一个以下列格式组合 df1、df2 和 df3 的 new_data_frame:

   match_up        result  team_df2  team_df3  win_df2  win_df3
0 1985_1116_1234 1 1116 1234 0.636 0.667
1 1985_1120_1345 1 1120 1345 0.621 0.742
2 1985_1207_1250 1 1207 1250 0.700 0.759
3 1985_1229_1425 1 1229 1425 0.615 0.667
4 1985_1242_1325 1 1242 1325 0.679 0.774
5 1986_1116_1430 0 1116 1430 0.742 0.434
6 1986_1250_ 1229 0 1250 1229 0.342 0.845
7 1986_1207_1437 1 1207 1437 0.567 0.123

我之前有这个问题here ,我也得到了很好的答案。但我面临的问题是,当年份值发生变化时(在数据框 df 中的 match_up 列中)团队值在 teamdf2 中的列和 df3不断重复。所以如果我在 team_df3 上合并这三个数据框和 team_df2 values 我没有得到想要的输出。

请帮我解决这个问题。操作等同于合并下图中的数据帧 1、2 和 3。但是match_up下图中第三个数据框中的列值变化如下:

A_515_729
.
.
.
B_767_890
.
.
.
P_390_789

enter image description here

最佳答案

拆分您的 match_up 列,以便我们将年份和其他 df id 作为单独的列:

In [23]:

df['year'] = list(map(int,(df['match_up'].str.split('_').str[0])))
df['team_df2'] = list(map(int,(df['match_up'].str.split('_').str[1])))
df['team_df3'] = list(map(int,(df['match_up'].str.split('_').str[2])))
df1['year'] = list(map(int,(df['match_up'].str.split('_').str[0])))
df2['year'] = list(map(int,(df['match_up'].str.split('_').str[0])))
df
Out[23]:
match_up result year team_df2 team_df3
0 1985_1116_1234 1 1985 1116 1234
1 1985_1120_1345 1 1985 1120 1345
2 1985_1207_1250 1 1985 1207 1250
3 1985_1229_1425 1 1985 1229 1425
4 1985_1242_1325 1 1985 1242 1325
5 1986_1116_1430 0 1986 1116 1430
6 1986_1250_1229 0 1986 1250 1229
7 1986_1207_1437 1 1986 1207 1437

现在我们可以使用年份和团队列进行合并以避免歧义:

In [24]:

merged = df.merge(df1, left_on=['year', 'team_df2'], right_on=['year','team_df2'])
merged = merged.merge(df2, left_on=['year', 'team_df3'], right_on=['year','team_df3'])
merged
Out[24]:
match_up result year team_df2 team_df3 win_df2 win_df3
0 1985_1116_1234 1 1985 1116 1234 0.636 0.667
1 1985_1120_1345 1 1985 1120 1345 0.621 0.742
2 1985_1207_1250 1 1985 1207 1250 0.700 0.759
3 1985_1229_1425 1 1985 1229 1425 0.615 0.667
4 1985_1242_1325 1 1985 1242 1325 0.679 0.774
5 1986_1116_1430 0 1986 1116 1430 0.742 0.434
6 1986_1250_1229 0 1986 1250 1229 0.342 0.845
7 1986_1207_1437 1 1986 1207 1437 0.567 0.123

然后您可以删除您不再感兴趣的列:

In [27]:

merged.drop('year',axis=1)
Out[27]:
match_up result team_df2 team_df3 win_df2 win_df3
0 1985_1116_1234 1 1116 1234 0.636 0.667
1 1985_1120_1345 1 1120 1345 0.621 0.742
2 1985_1207_1250 1 1207 1250 0.700 0.759
3 1985_1229_1425 1 1229 1425 0.615 0.667
4 1985_1242_1325 1 1242 1325 0.679 0.774
5 1986_1116_1430 0 1116 1430 0.742 0.434
6 1986_1250_1229 0 1250 1229 0.342 0.845
7 1986_1207_1437 1 1207 1437 0.567 0.123

关于python - 按数字组合 Pandas 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28985784/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com