BetterDocs
Home
Docs

Creation | pd.Series()

Method:

pd.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=<no_default>)

The constructor creates a one-dimensional labeled array, capable of holding any data type (integers, strings, floats, Python objects, etc.).

Returns:

pd.core.series.Series

Parameters:

data: (array-like, iterable, dict, scalar), Optional-

The data to populate the Series.

import pandas as pd

# Creating a Series
s = pd.Series(data=[10, 20, 30], index=['a', 'b', 'c'], name='Numbers')

print(s)
'''
Output:
a    10
b    20
c    30
Name: Numbers, dtype: int64
'''

If data is empty, it will return an empty Series.

index: str, Optional-

A sequence of labels for the data. If None, a default integer index (0, 1, 2, ...) is assigned.

import pandas as pd

# Creating a Series
s = pd.Series(data=[10, 20, 30], index=['x', 'y', 'z'], name='Numbers')

print(s)
'''
Output:
x    10
y    20
z    30
Name: Numbers, dtype: int64
'''

If index is provided, it must always match with the length of the data.

dtype: data-type, Optional-

Specifies the data-type of the Series. If not provided, it’s inferred from the input.

import pandas as pd

# Creating a Series
s = pd.Series(data=[10, 20, 30], index=['x', 'y', 'z'], dtype='int8', name='Numbers')

print(s)
'''
Output:
x    10
y    20
z    30
Name: Numbers, dtype: int8
'''

Values: +

name: str, Optional-

A name for the Series, which can be useful for identification.

import pandas as pd

# Creating a Series
s = pd.Series(data=[10, 20, 30], index=['x', 'y', 'z'], dtype='int8', name='Random Name')

print(s)
'''
Output:
x    10
y    20
z    30
Name: Random Name, dtype: int8
'''

copy: (True or False) Optional-

If True, a copy of the input data is created. This is useful if you want to ensure the original data remains unchanged.

copy = False (default) +

copy = True +

copy is handy only when the data is of the type np.ndarray.

Modification to the original_array or new_array will affect both the arrays.

Refer to numpy  documentation.

fastpath: <no_default>, Optional-

An internal parameter used to optimize Series creation.


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy