search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas | Series constructor

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas Series(~) constructor initialises a new Series.

Parameters

1. data | array-like or iterable or dict or scalar

The data to initialise the Series with.

2. indexlink | 1D array-like or index | optional

The index to use for the Series. The index does not have to be unique, that is, ["a","b","a"] is allowed.

3. dtypelink | string or numpy.dtype or ExtensionDtype | optional

The data type of the Series. By default, the data type is inferred from data.

4. namelink | string | optional

The name to assign to the Series.

5. copylink | boolean | optional

  • If True, then a new Series would be returned. Modifying this Series would not mutate the original data and vice versa.

  • If False, then the returned Series uses the same block of memory occupied by data. This means that modifying the Series would mutate the original data and vice versa.

copy=False only takes effect if data is a Numpy array or another Series. By default, copy=True.

Return Value

A Series object.

Examples

Using an array

To initialise a Series using an array:

s = pd.Series([3,4,5])
s
0 3
1 4
2 5
dtype: int64

Since we did not specify index, the default integer index was used ([0,1,2]).

Using a dictionary

To initialise a Series using a map:

my_data = {"A":3, "B":4, "C":5}
s = pd.Series(my_data)
s
A 3
B 4
C 5
dtype: int64

Here, notice how the key of the dictionary became the index of the Series.

Specifying index

To set the index of the Series, pass in the index parameter:

s = pd.Series([3,4,5], index=["A","B","C"])
s
A 3
B 4
C 5
dtype: int64

Specifying dtype

To enforce a data type on the Series:

s = pd.Series(["3","4","5"], dtype="int")
s
0 3
1 4
2 5
dtype: int64

Specifying name

To assign a name to the Series, pass in the name parameter:

s = pd.Series([3,4,5], name="my_series")
s
0 3
1 4
2 5
Name: my_series, dtype: int64

Specifying copy

By default, copy=True, which means that a new block of memory is allocated for the constructed Series. Modifying this Series will therefore not mutate the original data and vice versa.

Instead of creating a new copy of data, we can initialise a Series that uses the same memory block occupied by data:

s1 = pd.Series([3,4])
s2 = pd.Series(s1, copy=False)
s2
0 3
1 4
dtype: int64

Here, we are initialising a Series s2 from Series s1. The key is that these Series share the same block of memory for their data, which means that modifying one would mutate the other:

s1[0] = 9
s2
0 9
1 4
dtype: int64
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!