You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将pandas DataFrame中符合指定条件的两行合并为(2,17)形状的新DataFrame

实现方法

你只需要先把两次查询的结果分别存为变量,再用pd.concat()纵向拼接即可,完整代码如下:

import pandas as pd

df = pd.read_csv('all_time_olympic_medals.csv')
df2 = df.iloc[:-1]                      

# 分别存储两个筛选结果
summer_max_row = df2[df2['no_summer_golds'] == df2['no_summer_golds'].max()]
winter_max_row = df2[df2['no_winter_golds'] == df2['no_winter_golds'].max()]

# 纵向拼接得到目标DataFrame,ignore_index用于重置行索引
the_king_of_summer_winter_olympics = pd.concat([summer_max_row, winter_max_row], ignore_index=True)

拼接完成后执行验证语句即可确认符合要求:

print(the_king_of_summer_winter_olympics.shape)
# 输出:(2, 17)

内容的提问来源于stack exchange,提问作者PTP_XX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 12:54:06