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

如何将含1列2880个值的DataFrame转换为96行30列?

解决方法:将单列DataFrame转换为指定行列数的DataFrame

因为你的数据总量(2880)刚好等于目标行数×列数(96×30),所以直接通过重塑数组形状就能实现,这是最简便的方式。

具体代码示例

假设你的原始单列DataFrame名为df,操作如下:

import pandas as pd

# 重塑为96行30列的新DataFrame
new_df = pd.DataFrame(df.values.reshape(96, 30))

对应你给出的小例子验证

如果是1列10个元素转5行2列:

# 原始单列DataFrame
small_df = pd.DataFrame({'col': range(10)})
# 转换为5行2列
new_small_df = pd.DataFrame(small_df.values.reshape(5, 2))

补充说明

  • df.values会把DataFrame转为numpy数组,再通过reshape(行数, 列数)调整形状
  • 如果需要给新列命名,可以在创建新DataFrame时指定columns参数:
new_df = pd.DataFrame(df.values.reshape(96, 30), columns=[f'col_{i+1}' for i in range(30)])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:25:47