pandas 如何通过多次重复系列来创建数据框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24761220/
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 dataframe by repeating series multiple times?
提问by bbc
Is there any function like the following to create a dataframe with ten columns of Series s?
是否有类似以下的函数来创建一个包含十列 Series 的数据框?
df = pd.DataFrame(s, 10)
Thank you!
谢谢!
回答by EdChum
Use concat:
使用连接:
In [57]:
s = pd.Series(arange(10))
s
Out[57]:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: int32
In [59]:
pd.concat([s] * 10, axis=1)
Out[59]:
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9 9
If you want to append as rows then remove the axis=1param.
如果要附加为行,请删除axis=1参数。
回答by ZJS
I don't know of any pure numpy solution for what you want but you can use list comprehension and zip to transpose.
我不知道您想要什么纯 numpy 解决方案,但您可以使用列表理解和 zip 来转置。
df = pd.DataFrame(zip(*[s for i in range(10)]))
Without the comprehension...
没有领悟...
df = pd.DataFrame(zip(*[s]*10))

