33

I have 2 Series, given by:

import pandas as pd
r = pd.Series([i*3 for i in range(0, 10)], name='rrr')
s = pd.Series([i*5 for i in range(0, 10)], name='sss')

How to I create a DataFrame from them?

cottontail
  • 10,268
  • 18
  • 50
  • 51
KcFnMi
  • 5,516
  • 10
  • 62
  • 136

2 Answers2

62

You can use pd.concat:

pd.concat([r, s], axis=1)
Out: 
   rrr  sss
0    0    0
1    3    5
2    6   10
3    9   15
4   12   20
5   15   25
6   18   30
7   21   35
8   24   40
9   27   45

Or the DataFrame constructor:

pd.DataFrame({'r': r, 's': s})

Out: 
    r   s
0   0   0
1   3   5
2   6  10
3   9  15
4  12  20
5  15  25
6  18  30
7  21  35
8  24  40
9  27  45
drevicko
  • 14,382
  • 15
  • 75
  • 97
ayhan
  • 70,170
  • 20
  • 182
  • 203
  • 3
    don't forget you can order the columns in the DataFrame constructor with the `columns` argument https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html – Jason S Sep 21 '17 at 23:07
0

Another way is to the good old DataFrame constructor.

df = pd.DataFrame([r, s]).T

# or
df = pd.DataFrame({x.name: x for x in [r, s]})

res

cottontail
  • 10,268
  • 18
  • 50
  • 51