简体   繁体   English

1到100000如何创建Dataframe?

[英]How Do I Create a Dataframe from 1 to 100,000?

I am sure this is not hard, but I can't figure it out!我相信这并不难,但我想不通!

I want to create a dataframe that starts at 1 for the first row and ends at 100,000 in increments of 1, 2, 4, 5, or whatever.我想创建一个 dataframe,它从第一行的 1 开始,到 100,000 结束,增量为 1、2、4、5 或其他。 I could do this in my sleep in Excel, but is there a slick way to do this without importing a.csv or.txt file?我可以在 Excel 的睡眠中执行此操作,但是有没有一种巧妙的方法可以在不导入 .csv 或 .txt 文件的情况下执行此操作?

I have needed to do this in variations many times and just settled on importing a.csv, but I am tired of that.我需要多次执行此操作,最后决定导入 a.csv,但我对此感到厌倦。

Example in Excel Excel中的例子

Generating numbers生成数字

Generating numbers is not something special to pandas , rather numpy module or range function (as mentioned by @Grismer) can do the trick.生成数字并不是pandas的特殊之处,而是numpy模块或range function(如@Grismer 所述)可以做到这一点。 Let's say you want to generate a series of numbers and assign these numbers to a dataframe. As I said before, there are multiple approaches two of which I personally prefer.假设您要生成一系列数字并将这些数字分配给 dataframe。正如我之前所说,有多种方法,我个人更喜欢其中两种。

  • range function range function

Take range(1,1000,1) as an Example.range(1,1000,1)为例。 This function gets three arguments two of which are not mandatory.这个 function 得到三个 arguments 其中两个不是强制的。 The first argument defines the start number, the second one defines the end number, and the last one points to the steps of this range.第一个参数定义开始编号,第二个参数定义结束编号,最后一个指向这个范围的步骤。 So the abovementioned example will result in the numbers 1 to 9999 (Note that this range is a half-open interval which is closed at the start and open at the end).所以上面提到的例子将得到数字 1 到 9999(注意这个范围是一个半开区间,开始时关闭,结束时打开)。

  • numpy.arange function numpy.arange function

To have the same results as the previous example, take numpy.arange(1,1000,1) as an example.numpy.arange(1,1000,1)为例,要得到与上例相同的结果。 The arguments are completely the same as the range 's arguments. arguments 与range的 arguments 完全相同。

Assigning to dataframe分配给 dataframe

Now, if you want to assign these numbers to a dataframe, you can easily do this by using the pandas module.现在,如果您想将这些数字分配给 dataframe,您可以使用pandas模块轻松完成此操作。 Code below is an example of how to generate a dataframe:下面的代码是如何生成 dataframe 的示例:

import numpy as np
import pandas as pd
myRange = np.arange(1,1001,1) # Could be something like myRange = range(1,1000,1)
df = pd.DataFrame({"numbers": myRange})
df.head(5)

which results in a dataframe like(Note that just the first five rows have been shown):结果是 dataframe(注意只显示了前五行):

numbers数字
0 0 1 1个
1 1个 2 2个
2 2个 3 3个
3 3个 4 4个
4 4个 5 5个

Difference of numpy.arange and range numpy.arangerange的区别

To keep this answer short, I'd rather to refer to this answer by @hpaulj为了使这个答案简短,我宁愿引用@hpaulj 的这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM