bbc
bbc

Reputation: 1101

how to create a dataframe by repeating series multiple times?

Is there any function like the following to create a dataframe with ten columns of Series s?

df = pd.DataFrame(s, 10)

Thank you!

Upvotes: 9

Views: 12993

Answers (2)

EdChum
EdChum

Reputation: 393923

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=1 param.

Upvotes: 22

ZJS
ZJS

Reputation: 4051

I don't know of any pure numpy solution for what you want but you can use list comprehension and zip to transpose.

df = pd.DataFrame(zip(*[s for i in range(10)]))

Without the comprehension...

df = pd.DataFrame(zip(*[s]*10))

Upvotes: 0

Related Questions