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

Python中将按日期分组的DataFrame转换为指定行格式

解决方法

你可以通过以下两种方式实现格式转换:

方式一:遍历分组直接生成目标格式

for date, group in grouped:
    # 提取日期并去掉时分秒部分
    date_str = date.strftime('%Y-%m-%d')
    # 将收盘价列转为字符串列表
    close_values = group['close'].astype(str).tolist()
    # 拼接并输出目标格式内容
    print(f"{date_str} {' '.join(close_values)}")

方式二:用聚合函数批量生成

# 聚合每个分组生成目标字符串
formatted_rows = grouped['close'].agg(
    lambda x: f"{x.name.strftime('%Y-%m-%d')} {' '.join(x.astype(str))}"
)
# 逐行输出结果
for row in formatted_rows:
    print(row)

运行后会输出你需要的格式:

2011-01-07 16.42 44.36 69.38 14.25 93.73 20.97
2011-01-14 15.97 46.25 70.07 15.25 94.01 21.21

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 18:01:06