如何创建具有指定行数和列数的 Pandas DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53104048/
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
How to create a pandas DataFrame with specified number of rows and columns
提问by Bhargav
I'm new to pandas concept, Is it possible to create a DataFrame of size 1 row and column-length of 8.
我是Pandas概念的新手,是否可以创建大小为 1 行和列长为 8 的 DataFrame。
I tried:
我试过:
import pandas as pd
df = pd.DataFrame({'Data':[]})
but this only creates one row and one column.
但这只会创建一行和一列。
回答by user3483203
You can specify both index
and columns
to determine the shape. Values will default to NaN
.
您可以同时指定index
和columns
来确定形状。值将默认为NaN
.
pd.DataFrame(index=np.arange(1), columns=np.arange(8))
0 1 2 3 4 5 6 7
0 NaN NaN NaN NaN NaN NaN NaN NaN
回答by rahlf23
Yes, it is possible to create a dataframe of any shape. For example:
是的,可以创建任何形状的数据框。例如:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,10, size=(1,8)))
Yields:
产量:
0 1 2 3 4 5 6 7
0 1 5 2 3 4 8 7 1
Then we can return the shape of this dataframe using df.shape
:
然后我们可以使用以下命令返回此数据框的形状df.shape
:
(1, 8)