Pandas Series from a DataFrame


This entry is part 2 of 4 in the series pandas Series

Can you create a pandas Series from a pandas DataFrame? Yes.

A column in a DataFrame can be retrieved as a Series either by dictionary-like notation or by using dot attribute notation.

Let’s manually create a DataFrame in pandas and name our DataFrame df. I will use a dictionary to create a DataFrame.

import pandas as pd  # import the pandas library into Python
data = {'firstname': ['Bob', 'Sally', 'Suzie', 'Rohan'],
       'amount': [12, 67, 33, 41]}
df = pd.DataFrame(data)
df

type(df)
pandas.core.frame.DataFrame

Create a series from a column in the DataFrame.

ser = df['firstname']
ser
0      Bob
1    Sally
2    Suzie
3    Rohan
Name: firstname, dtype: object
type(ser)
pandas.core.series.Series

How do you create a DataFrame from just one column in a DataFrame? You can subset specific columns of a DataFrame object by using double square brackets: [[]] and listing the columns in between, separated by commas.

Series Navigation<< Pandas Series IntroductionPandas Series from Dictionary >>

Leave a comment

Your email address will not be published. Required fields are marked *