Python 创建特定大小的熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45473330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a pandas data frame of a specific size
提问by user1357015
In R, I can do something like this:
在 R 中,我可以做这样的事情:
myvec <- seq(from = 5, to = 10)^2
mydf <- data.frame(matrix(data = myvec, ncol = 3,byrow = TRUE))
> mydf
X1 X2 X3
1 25 36 49
2 64 81 100
Notice I can specfiy the shape of the data frame by passing in an ncol parameter. I can then fill it either byrow or bycolumn (in this case by row).
请注意,我可以通过传入 ncol 参数来指定数据框的形状。然后我可以按行或按列(在这种情况下按行)填充它。
If I were to replicate this in Python/Pandas, it's easy enough to create the sequence:
如果我要在 Python/Pandas 中复制它,创建序列很容易:
myData = [x**2 for x in range(5,11) ]
However, how do easily make a dataframe of the same size? I can do something like:
但是,如何轻松制作相同大小的数据框?我可以做这样的事情:
myDF = pd.DataFrame(data = myData)
But what would be the parameters to specify the column/row dimensions?
但是指定列/行维度的参数是什么?
回答by Kevinj22
One way to make a pandas dataframe of the size you wish is to provide index and column values on the creation of the dataframe.
制作所需大小的熊猫数据框的一种方法是在创建数据框时提供索引和列值。
df = pd.DataFrame(index=range(numRows),columns=range(numCols))
This creates a dataframe full of nan's where all columns are of data type object.
这将创建一个充满 nan 的数据框,其中所有列都是数据类型对象。
回答by unutbu
Use reshape
to specify the number of columns (or rows):
使用reshape
指定的列(或行)的数量:
import numpy as np
import pandas as pd
myvec = np.arange(5, 11)**2
mydf = pd.DataFrame(myvec.reshape(-1, 3))
yields
产量
0 1 2
0 25 36 49
1 64 81 100
When calling reshape
you are allowed to specify the length of one axis as -1
.
reshape
replaces the -1
with whatever integer makes sense. For example, if myvec.size
is 6, and one axis is of length 3, then the other axis has to be of length 6/3 = 2. So the -1
is replaced by 2, and so myvec.reshape(-1, 3)
returns an array of shape (2, 3)
-- 2 row and 3 columns.
调用时,reshape
您可以将一个轴的长度指定为-1
.
用任何有意义的整数reshape
替换 the -1
。例如,如果myvec.size
是 6,并且一个轴的长度为 3,那么另一个轴的长度必须为 6/3 = 2。所以-1
被 2 替换,因此myvec.reshape(-1, 3)
返回一个形状数组——2(2, 3)
行和 3列。