You can also try this :
df = DataFrame(series).transpose()
Using the transpose() function you can interchange the indices and the columns. The output looks like this :
a b c
0 1 2 3
Answer from PJay on Stack OverflowYou can also try this :
df = DataFrame(series).transpose()
Using the transpose() function you can interchange the indices and the columns. The output looks like this :
a b c
0 1 2 3
You don't need the transposition step, just wrap your Series inside a list and pass it to the DataFrame constructor:
pd.DataFrame([series])
a b c
0 1 2 3
Alternatively, call Series.to_frame, then transpose using the shortcut .T:
series.to_frame().T
a b c
0 1 2 3
Videos
Here is how to create a DataFrame where each series is a row.
For a single Series (resulting in a single-row DataFrame):
Copyseries = pd.Series([1,2], index=['a','b'])
df = pd.DataFrame([series])
For multiple series with identical indices:
Copycols = ['a','b']
list_of_series = [pd.Series([1,2],index=cols), pd.Series([3,4],index=cols)]
df = pd.DataFrame(list_of_series, columns=cols)
For multiple series with possibly different indices:
Copylist_of_series = [pd.Series([1,2],index=['a','b']), pd.Series([3,4],index=['a','c'])]
df = pd.concat(list_of_series, axis=1).transpose()
To create a DataFrame where each series is a column, see the answers by others. Alternatively, one can create a DataFrame where each series is a row, as above, and then use df.transpose(). However, the latter approach is inefficient if the columns have different data types.
No need to initialize an empty DataFrame (you weren't even doing that, you'd need pd.DataFrame() with the parens).
Instead, to create a DataFrame where each series is a column,
- make a list of Series,
series, and - concatenate them horizontally with
df = pd.concat(series, axis=1)
Something like:
Copyseries = [pd.Series(mat[name][:, 1]) for name in Variables]
df = pd.concat(series, axis=1)
Rather than create 2 temporary dfs you can just pass these as params within a dict using the DataFrame constructor:
pd.DataFrame({'email':sf.index, 'list':sf.values})
There are lots of ways to construct a df, see the docs
to_frame():
Starting with the following Series, email_series:
email
[email protected] A
[email protected] B
[email protected] C
dtype: int64
email_series = pd.Series(["[email protected]", ...])
I use to_frame to convert the series to DataFrame:
df = email_series.to_frame().reset_index()
email 0
0 [email protected] A
1 [email protected] B
2 [email protected] C
3 [email protected] D
Now all you need is to rename the column name and name the index column:
df = df.rename(columns= {0: 'list'})
df.index.name = 'index'
Your DataFrame is ready for further analysis.
Update: I just came across this link where the answers are surprisingly similar to mine here.