Table of Contents
To create a Pandas DataFrame from a Series, you can use the “pd.DataFrame()” function in Python. This function takes in a Series object as its input and converts it into a tabular data structure with labeled rows and columns. Some examples of creating a DataFrame from a Series include using a dictionary, a list, or a numpy array as the data source for the Series. Once the Series is created, it can be passed as an argument to the DataFrame function to create the desired DataFrame. This process allows for easy manipulation and analysis of data in a tabular format.
Create Pandas DataFrame from Series (With Examples)
Often you may want to create a pandas DataFrame from one or more pandas Series.
The following examples show how to create a pandas DataFrame using existing series as either the rows or columns of the DataFrame.
Example 1: Create Pandas DataFrame Using Series as Columns
Suppose we have the following three pandas Series:
import pandas as pd #define three Series name = pd.Series(['A', 'B', 'C', 'D', 'E']) points = pd.Series([34, 20, 21, 57, 68]) assists = pd.Series([8, 12, 14, 9, 11])
We can use the following code to convert each series into a DataFrame and then concatenate them all into one DataFrame:
#convert each Series to a DataFrame
name_df = name.to_frame(name='name')
points_df = points.to_frame(name='points')
assists_df = assists.to_frame(name='assists')
#concatenate three Series into one DataFrame
df = pd.concat([name_df, points_df, assists_df], axis=1)
#view final DataFrame
print(df)
name points assists
0 A 34 8
1 B 20 12
2 C 21 14
3 D 57 9
4 E 68 11Notice that the three series are each represented as columns in the final DataFrame.
Example 2: Create Pandas DataFrame Using Series as Rows
Suppose we have the following three pandas Series:
import pandas as pd #define three Series row1 = pd.Series(['A', 34, 8]) row2 = pd.Series(['B', 20, 12]) row3 = pd.Series(['C', 21, 14])
We can use the following code to combine each of the Series into a pandas DataFrame, using each Series as a row in the DataFrame:
#create DataFrame using Series as rows
df = pd.DataFrame([row1, row2, row3])
#create column names for DataFrame
df.columns = ['col1', 'col2', 'col3']
#view resulting DataFrame
print(df)
col1 col2 col3
0 A 34 8
1 B 20 12
2 C 21 14
Notice that the three series are each represented as rows in the final DataFrame.
Additional Resources
The following tutorials explain how to perform other common operations in Python:
Cite this article
stats writer (2024). How do I create a Pandas DataFrame from a Series? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-create-a-pandas-dataframe-from-a-series-can-you-provide-some-examples/
stats writer. "How do I create a Pandas DataFrame from a Series? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-i-create-a-pandas-dataframe-from-a-series-can-you-provide-some-examples/.
stats writer. "How do I create a Pandas DataFrame from a Series? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-create-a-pandas-dataframe-from-a-series-can-you-provide-some-examples/.
stats writer (2024) 'How do I create a Pandas DataFrame from a Series? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-create-a-pandas-dataframe-from-a-series-can-you-provide-some-examples/.
[1] stats writer, "How do I create a Pandas DataFrame from a Series? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How do I create a Pandas DataFrame from a Series? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
