In this
post, I am going to deep dive into the Pandas Series constructor.
Following
table summarizes the parameters of Series constructor.
|
Parameter
|
Description
|
|
data
|
Specifies
the data that we want to include in the Series. data can be a Python list, NumPy
array, dictionary, scalar value, or another Series.
|
|
index
|
Index
argument allows you to specify custom labels for the elements of the series.
|
|
dtype
|
Specify
the data type of the series
|
|
copy
|
If copy
is set to True, then Pandas create a Series from an existing object by
copying the data. 'copy' argument is set to Tru by default.
If copy
is set to False, then the original data is shared with Series object, If you
modify the original object, the it modifies the Series.
|
|
name
|
Set the name
to a Pandas Series.
|
data
argument
We can
specify the value to 'data' parameter directly in Series method.
series_from_list = pd.Series(data=[2, 3, 5, 7])
Mentioning
the name ‘data’ is optional, following statement do the same.
series_from_list = pd.Series([2, 3, 5, 7])
data_argument.py
import pandas as pd
import numpy as np
series_from_list = pd.Series([2, 3, 5, 7])
series_from_list_1 = pd.Series(data=[2, 3, 5, 7])
series_from_numpy_array = pd.Series(np.array([2, 3, 5, 7]))
series_from_dict = pd.Series({'FirstPrime' : 2, 'SecondPrime' : 3, 'ThirdPrime' : 5})
series_from_scalar = pd.Series(2)
series_from_another_series = pd.Series(series_from_list)
print('series_from_list:\n',series_from_list)
print('\nseries_from_list_1:\n',series_from_list_1)
print('\nseries_from_numpy_array:\n',series_from_numpy_array)
print('\nseries_from_dict:\n',series_from_dict)
print('\nseries_from_scalar:\n',series_from_scalar)
print('\nseries_from_another_series:\n',series_from_another_series)
Output
series_from_list:
0 2
1 3
2 5
3 7
dtype: int64
series_from_list_1:
0 2
1 3
2 5
3 7
dtype: int64
series_from_numpy_array:
0 2
1 3
2 5
3 7
dtype: int64
series_from_dict:
FirstPrime 2
SecondPrime 3
ThirdPrime 5
dtype: int64
series_from_scalar:
0 2
dtype: int64
series_from_another_series:
0 2
1 3
2 5
3 7
dtype: int64
index
argument
Example
data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series = pd.Series(data, index)
index_argument.py
import pandas as pd
data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series = pd.Series(data, index)
print(series)
Output
first_prime 2
second_prime 3
third_prime 5
fourth_prime 7
dtype: int64
dtype
argument
Example
1: Data type is
derived from the data by default
data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series1 = pd.Series(data, index)
In the
above example, Pandas deduce data type as int64
first_prime 2
second_prime 3
third_prime 5
fourth_prime 7
dtype: int64
Example
2: Specify the data
type explicitly.
series2 = pd.Series(data, index, dtype='float64')
In the
above example, I set the data type as float64 explicitly.
dtype_argument.py
import pandas as pd
data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series1 = pd.Series(data, index)
series2 = pd.Series(data, index, dtype='float64')
print(series1,'\n')
print(series2)
copy
argument
If copy is
set to True, then Pandas create a Series from an existing object by copying the
data.
If copy is
set to False, then the original data is shared with Series object, If you
modify the original object, the it modifies the Series.
name
argument
name_argument.py
import pandas as pd
series_1 = pd.Series([2, 3, 5, 7, 11], name='five_primes')
print(series_1)
Output
0 2
1 3
2 5
3 7
4 11
Name: five_primes, dtype: int64
Previous
Next
Home