python - 将数据拆分为每 10 个实例并与新数据框连接
问题描述
我有一个数据框 =
Lat Lon COG Sog status
16.23 23.92 20 14 1004
数据集的长度为 540x5
现在我需要将数据从 row[0] 拆分到 row[9] 即 10x5 并存储到一个新的 DataFrame 中。接下来将数据从row[1]拆分到row[10],即s1,下一个数据从row[2]拆分到row[11],即s2,最多540次迭代。然后连接所有的 DataFrame 并存储到一个新的 DataFrame 中。
解决方案
您可以尝试以下方法:
设置:
np.random.seed(123)
df = pd.DataFrame(np.random.randint(0,100,(20,4)),columns=list('ABCD'))
使用列表推导,我们得到如下索引:
[(a,b) for a, b in zip(df.index,df.index[10:])]
#[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14),
#(5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]
同样,只需传递df.iloc
then concat 下的索引:
win = 10 #change the window as required
final = pd.concat([df.iloc[a:b] for a, b in zip(df.index,df.index[win:])],sort=False)
print(final)
推荐阅读
- python - 从几乎每周到每天重新采样熊猫数据框
- python-3.x - 如何使用 Linux alpine 3.13 在 Docker 映像上安装 hdf5
- flutter - 扑。ColorFiltered 点击它
- azure-devops - 有什么方法可以在 Azure Devops 的发布管道中获取 TriggeredBy 值
- jenkins - GitHub webhook 无法连接到 VPN 内的 Jenkins?
- react-native - 如何在反应原生中拖动视图
- android - 升级 Flutter 会导致这个错误:“当前 Flutter 版本是 0.0.0-unknown”
- automation - CANopen 中的 PDO 链接
- google-apps-script - 将 Excel 转换为 Gsheet,然后在不需要 Auth 的情况下导入它们
- python - 我正在尝试找到平均速度并想要使用 Python 的最简单方法