HOWTO · Pandas

How to Convert Pandas Series to DataFrame

It introduces how to convert pandas series to dataframe

On this page

Use pandas.DataFrame() or pandas.Series.to_frame() to convert one Pandas Series into a DataFrame. Both methods retain the Series index as the DataFrame row index. Use to_frame(name=...) to name its value column; use reset_index(name=...) when the old index should become a regular column. To combine several Series as columns, use pd.concat(..., axis=1).

The examples use this Series, whose labels become the row index in each conversion:

import pandas as pd
import numpy as np

np.random.seed(0)

df_series = pd.Series(
    np.random.randint(0, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
)

print(df_series)
a    44
b    47
c    64
d    67
e    67
f     9
g    83
h    21
i    36
j    87
dtype: int64

Convert a Single Pandas Series to DataFrame Using pandas.DataFrame()

Pass a Series to pandas.DataFrame() to create a one-column DataFrame. The columns argument sets the value column’s label; the Series index remains the row index. This general constructor is useful when you are already building a DataFrame from other data.

import pandas as pd
import numpy as np

np.random.seed(0)

df_series = pd.Series(
    np.random.randint(0, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
)

print(pd.DataFrame(df_series, columns=["A"]))
    A
a  44
b  47
c  64
d  67
e  67
f   9
g  83
h  21
i  36
j  87

The resulting DataFrame has one column named A and keeps the labels a through j as its index.

Use the constructor when the input is already a Series and you want to specify DataFrame options at creation. The columns list controls the output column label; it does not replace the Series index. If you omit columns, pandas uses the Series name for the value column when one is set, and otherwise uses 0.

Convert a Single Pandas Series to DataFrame Using pandas.Series.to_frame()

Call Series.to_frame() when you want to turn a Series directly into a one-column DataFrame. Pass name to choose the value column’s label. The method keeps the Series index, which is useful when those labels identify the rows.

For a single Series, to_frame(name="A") is a concise alternative to pandas.DataFrame(series, columns=["A"]). Choose a name that describes the values, especially before joining this DataFrame with other tables. These conversions do not turn the Series index into a data column; use reset_index() for that layout.

import pandas as pd
import numpy as np

np.random.seed(0)

df_series = pd.Series(
    np.random.randint(0, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
)

print(df_series.to_frame(name="A"))
    A
a  44
b  47
c  64
d  67
e  67
f   9
g  83
h  21
i  36
j  87

If a Series has no name, to_frame() uses 0 as the value column label. When you also need the Series index as data in a column, use the reset_index() method. Naming the index axis makes the first output column index:

import pandas as pd
import numpy as np

np.random.seed(0)

df_series = pd.Series(
    np.random.randint(0, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
).rename_axis("index")

print(df_series)
print("\n")
print(df_series.reset_index())
index
a    44
b    47
c    64
d    67
e    67
f     9
g    83
h    21
i    36
j    87
dtype: int64


  index   0
0     a  44
1     b  47
2     c  64
3     d  67
4     e  67
5     f   9
6     g  83
7     h  21
8     i  36
9     j  87

reset_index() returns a DataFrame with the former index in the index column and the Series values in a column named 0. Supply name to label the value column directly:

import pandas as pd
import numpy as np

np.random.seed(0)

df_series = pd.Series(
    np.random.randint(0, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
).rename_axis("index")

print(df_series)
print("\n")
print(df_series.reset_index(name="A"))
index
a    44
b    47
c    64
d    67
e    67
f     9
g    83
h    21
i    36
j    87
dtype: int64


  index   A
0     a  44
1     b  47
2     c  64
3     d  67
4     e  67
5     f   9
6     g  83
7     h  21
8     i  36
9     j  87

Convert Multiple Pandas Series to Dataframes

To place multiple Series side by side in one DataFrame, pass them to pd.concat() with axis=1. Pandas aligns values by index label, so matching labels share a row. The Series below have the same index and produce three value columns:

import pandas as pd
import numpy as np

np.random.seed(0)

df_series1 = pd.Series(
    np.random.randint(0, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
)
df_series2 = pd.Series(
    np.random.randint(40, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
)
df_series3 = pd.Series(
    np.random.randint(80, 100, size=(10)),
    index=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
)

df_stitched = pd.concat([df_series1, df_series2, df_series3], axis=1)

print(df_stitched)
    0   1   2
a  44  46  97
b  47  64  85
c  64  64  93
d  67  52  88
e  67  98  89
f   9  41  99
g  83  78  96
h  21  79  99
i  36  63  85
j  87  86  95

The pd.concat() function uses axis=1 to concatenate along columns. Its default is axis=0, which concatenates along rows instead. When indexes differ, column-wise concatenation aligns on their labels; labels absent from a Series have missing values in that column.