首页 \ 问答 \ 在pandas数据帧中复制数据(Replicate the data in pandas dataframe)

在pandas数据帧中复制数据(Replicate the data in pandas dataframe)

我在数据帧df有一些数据,其长度为n ,我正在创建一个更大的数据帧dg其长度为10n 。 我想将数据从df复制到dg以便dg中的行定期填充df的数据。 我试过以下:

dg = pd.DataFrame(index = range(10*n), columns = columns)

for i in range(0, 10*n, n):
    for j in range(n):
        dg[col][i : i+n] = df[col][0:n]

但是,这非常慢。 有没有更快的方法来实现相同的目标? 理想情况下,我希望看到一个解决方案,我可以简单地使用df并将其长度扩展到10n,这样所有数据都可以定期复制。


I have some data in a dataframe df whose length is n and I am creating a larger dataframe dg whose length is say 10n. I want to copy data from df to dg so that the rows in dg would be periodically filled by the data in df. I tried following:

dg = pd.DataFrame(index = range(10*n), columns = columns)

for i in range(0, 10*n, n):
    for j in range(n):
        dg[col][i : i+n] = df[col][0:n]

However, this is extremely slow. Is there any faster way to achieve the same? Ideally, I would love to see a solution in which I can simply take df and extend its length to 10n so that all the data would simply be copied periodically.


原文:https://stackoverflow.com/questions/42592512
更新时间:2020-03-26 12:20

最满意答案

如果你不关心订单,那么这应该工作:

import pandas as pd
x = pd.DataFrame({"data": [1,2]})
df = pd.concat([x]*5, ignore_index=True)
df

输出:

    data
0   1
1   2
2   1
3   2
4   1
.
.

如果您关心订单,那么您可以采用这种方法:

import numpy as np
df = x.loc[np.repeat(x.index.values, 3)]
df

输出:

    data
0   1
0   1
0   1
1   2
1   2
1   2

if you don't care about order then this should work:

import pandas as pd
x = pd.DataFrame({"data": [1,2]})
df = pd.concat([x]*5, ignore_index=True)
df

output:

    data
0   1
1   2
2   1
3   2
4   1
.
.

if you care about the order then you can go with this approach:

import numpy as np
df = x.loc[np.repeat(x.index.values, 3)]
df

output:

    data
0   1
0   1
0   1
1   2
1   2
1   2
2017-03-04

相关文章

更多

最新问答

更多
  • css在元素之前中断列而不破坏包装器(css break column before element without breaking the wrapper)
  • 如何在Xamarin共享项目中使用自定义渲染器(How to use Custom Renderer in Xamarin Shared Project)
  • 如何为特定表中的特定字段设置唯一?(How to set unique for specific field from specific table?)
  • Google SDK iOS - sign()方法完成处理程序(Google SDK iOS - sign() method completion handler)
  • 在具有接口{}值的地图上实现String()(Implement String() on a map with interface{} values)
  • 检查数据库中是否已存在用户名(Check if username already exist in DB)
  • 使用javascript进行ajax调用时阻止用户交互(Block user interaction while doing ajax call using javascript)
  • 什么'if(err)'在Javascript中精确测试?(What does 'if (err)' tests precisely in Javascript?)
  • jQuery mouseleave无法正常工作(jQuery mouseleave not working)
  • 寻求使用的一些说明(Seeking some clarification on use of )
  • 将数组传递给注释的语法(syntax for passing array to annotation)
  • 用于从两个日期范围之间的文件中提取数据的Shell脚本(Shell script to extract data from file between two date ranges)
  • 元素隐藏但父()没有(Element hides but parent() not)
  • 如何使用Google App Engine Java平台开发web ui(How to develop web ui with Google App Engine Java platform)
  • 对于OWL A级;(For an OWL class A; Getting all properties that A is their domain)
  • Excel VBA公式格式问题(Excel VBA Formula Format Issue)
  • ORA - 02287序列号不允许在这里(ORA - 02287 sequence number not allowed here)
  • Github拉忽略特定文件(Github Pull Ignore Specific File)
  • SQL CONVERT函数在SQL Server中工作但不在应用程序中(SQL CONVERT function working in SQL Server but not in application)
  • backbone.js适用于大型应用程序(backbone.js for large applications)
  • 防止程序关闭(Preventing program from closing)
  • 生成不带图像的heightMap(Generating a heightMap without an Image)
  • Bootstrap - 如何将包含文本的div居中?(Bootstrap - How to center div that has text inside it?)
  • Android - 片段findViewById()总是null?(Android - Fragment findViewById() always null?)
  • 确定CSS中的高度(Figuring out heights in CSS)
  • 使用__autoload包含类和使用命名空间(Use __autoload to include class and use namespace)
  • setTimeout()不允许我传递文本值[重复](setTimeout() doesn't allow me to pass text values [duplicate])
  • 在NSUserDefault中恢复值(Restoring value in NSUserDefault)
  • 知道如何将这种下沉的悬停效果添加到图像/链接吗?(Any idea how to add this sinking hover effect to an image/link?)
  • 在XIB中淡入/淡出UISegmentedControl(fade in/fade out UISegmentedControl in XIB)