241

Why does pandas make a distinction between a Series and a single-column DataFrame?
In other words: what is the reason of existence of the Series class?

I'm mainly using time series with datetime index, maybe that helps to set the context.

6
  • Well they are different obviously, I think you are referring to certain operations that still return a dataframe either because you only have a single column dataframe or because the operation results in a single column dataframe. However when selecting a single column there is no ambiguity and this decomposes to a Series. You have to show sample code to explain what your issue is.
    – EdChum
    Sep 25, 2014 at 20:16
  • 10
    The main issue is that I don't see the need for a Series object, with different methods.
    – saroele
    Sep 25, 2014 at 20:22
  • For one, there is a namespace difference. Series have only a top level name, dataframes have a top level and a column name. That can lead to significant differences in syntax for processing/creating a new series vs a new column.
    – JohnE
    Sep 25, 2014 at 22:36
  • 7
    As far as I can tell, this question should still be answered. Even though one can think of DataFrame as a dict of Series (though that isn't the current implementation), it's still unclear why you would ever return a Series object instead of a DataFrame (ie conceptually a dict with one entry).
    – Alex
    Jun 22, 2018 at 6:11
  • 1
    @EdChum well that is not true. Consider dataframe.loc[:, string] vs dataframe.loc[:, [string]]: one column is selected in either case but returned type is, respectively, Series and DataFrame. Mar 31, 2022 at 10:20

6 Answers 6

264
+50

Quoting the Pandas docs

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

Two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. Can be thought of as a dict-like container for Series objects. The primary pandas data structure.

So, the Series is the data structure for a single column of a DataFrame, not only conceptually, but literally, i.e. the data in a DataFrame is actually stored in memory as a collection of Series.

Analogously: We need both lists and matrices, because matrices are built with lists. Single row matricies, while equivalent to lists in functionality still cannot exist without the list(s) they're composed of.

They both have extremely similar APIs, but you'll find that DataFrame methods always cater to the possibility that you have more than one column. And, of course, you can always add another Series (or equivalent object) to a DataFrame, while adding a Series to another Series involves creating a DataFrame.

9
  • 3
    Thanks for your answer. My question was inspired by a bug in my code when a selection on a dataframe suddenly returned a series instead and I could not access the columns attribute. I'm not the only one confused: stackoverflow.com/questions/16782323/…
    – saroele
    Oct 13, 2014 at 7:51
  • 12
    You cannot conclude anything about the actual internal data structure of a DataFrame from Can be thought of as a dict-like container for Series objects. In fact, it's currently stored as a BlockManager (which is an implementation detail you should not rely on).
    – Tim Diels
    Dec 22, 2015 at 15:26
  • 21
    I may be pedantic, but I don't see the OP's question of WHY series exist answered. I see an answer describing the relationship between series and dataframes, but not an answer explaining why we should want to have a series as a separate data type as opposed to being a special case of a dataframe (namely one with only one column). Jan 10, 2019 at 12:56
  • 4
    So, why would one ever use a Series over a Dataframe?
    – Gulzar
    Jan 27, 2021 at 12:17
  • 2
    IMO, this argues that Series is a leaky abstraction, and argues that Series should not be exposed to the user. Instead, one column DataFrames would be more useful.
    – rsmith54
    Sep 23, 2021 at 20:03
15

from the pandas doc http://pandas.pydata.org/pandas-docs/stable/dsintro.html Series is a one-dimensional labeled array capable of holding any data type. To read data in form of panda Series:

import pandas as pd
ds = pd.Series(data, index=index)

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.

import pandas as pd
df = pd.DataFrame(data, index=index)

In both of the above index is list

for example: I have a csv file with following data:

,country,popuplation,area,capital
BR,Brazil,10210,12015,Brasile
RU,Russia,1025,457,Moscow
IN,India,10458,457787,New Delhi

To read above data as series and data frame:

import pandas as pd
file_data = pd.read_csv("file_path", index_col=0)
d = pd.Series(file_data.country, index=['BR','RU','IN'] or index =  file_data.index)

output:

>>> d
BR           Brazil
RU           Russia
IN            India

df = pd.DataFrame(file_data.area, index=['BR','RU','IN'] or index = file_data.index )

output:

>>> df
      area
BR   12015
RU     457
IN  457787
2
  • 2
    I didn't downvote, but your code doesn't work. You may want to change file_data to brics, add a US line to the csv, and change ['BR'....'US'] to brics.index. Perhaps correct pupuplation.
    – RolfBly
    Jul 26, 2018 at 14:04
  • @RolfBly: Thank you for pointing out those mistakes. It was silly on my part to make them. I have changed them. Thank you! And regarding reading that just an example I took hence random values. Jul 27, 2018 at 9:58
3

Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index. The basic method to create a Series is to call:

s = pd.Series(data, index=index)

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects.

 d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
 two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
 df = pd.DataFrame(d)
3

Series is a one-dimensional object that can hold any data type such as integers, floats and strings e.g

   import pandas as pd
   x = pd.Series([A,B,C]) 

0 A
1 B
2 C

The first column of Series is known as index i.e 0,1,2 the second column is your actual data i.e A,B,C

DataFrames is two-dimensional object that can hold series, list, dictionary

df=pd.DataFrame(rd(5,4),['A','B','C','D','E'],['W','X','Y','Z'])
1
  • that does not answer the OPs' question Dec 29, 2022 at 14:58
0

A DataFrame is often described as

a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table

Because of this definition, we may look at the data as cells, like in an Excel spreadsheet, which has rows with a row number and and columns with a column header. Because of this simplistic view, the underlying data structure may be a bit surprising.

The DataFrame actually consists of Index objects for the axes labels (row and column labels) and Series objects for the column data.

The Series object provides encapsulation for each of the column data (which is held in a one dimensional numpy.ndarray), with row labels and the column label.

So a single column DataFrame will have one underlying Series object for the column data.

See DataFrame data structure for a simple example with a visual representation of the underlying data structure.

-1

Import cars data

import pandas as pd

cars = pd.read_csv('cars.csv', index_col = 0)

Here is how the cars.csv file looks.

Print out drives_right column as Series:

print(cars.loc[:,"drives_right"])

    US      True
    AUS    False
    JAP    False
    IN     False
    RU      True
    MOR     True
    EG      True
    Name: drives_right, dtype: bool

The single bracket version gives a Pandas Series, the double bracket version gives a Pandas DataFrame.

Print out drives_right column as DataFrame

print(cars.loc[:,["drives_right"]])

         drives_right
    US           True
    AUS         False
    JAP         False
    IN          False
    RU           True
    MOR          True
    EG           True

Adding a Series to another Series creates a DataFrame.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.