Sunday, 3 September 2023

A Guide to the Pandas Series Constructor

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

# data can be a Python list, NumPy array, dictionary, scalar value, or another Series.
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

Convert Pandas Series to NumPy Array in Python

 sing series ‘values’ attribute, we can get the array of values contained in the Series. The return type of values attribute is the NumPy array

 

get_series_values.py

import pandas as pd

primes = [2, 3, 5]
primes_index = ['first_prime', 'second_prime', 'third_prime']
primes_series = pd.Series(primes, index=primes_index, name='Hold the Prime numbers')

primes_series_values = primes_series.values

print('primes_series_values : ', primes_series_values)
print('type of primes_series_values : ', type(primes_series_values))

 

Output

primes_series_values :  [2 3 5]
type of primes_series_values :  <class 'numpy.ndarray'>

 

 

 

 

Previous                                                 Next                                                 Home

How to Split a PlantUML Diagram into Multiple Images?

page (hpages)x(vpages) command split the image into multiple pages.

 

hpages: Specifies number of horizontal pages

vpages: Specifies number of vertical pages.

 

splitDiagramIntoPages.txt

@startuml

	page 2x2

	skinparam pageMargin 10
	skinparam pageExternalColor gray
	skinparam pageBorderColor black

	package "java.lang" #lightblue {
		interface Appendable
		interface AutoCloseable
		interface Cloneable

		class Boolean
		class System
		class Object

		Object <|-- Boolean
		Object <|-- System

	}

	package "java.util" #F0F0F0{
		interface Collection<E>
		interface List<E>

		class ArrayList<E> implements List
		Object <|-- ArrayList
	}
	
@enduml

 

Above snippet generate 4 image files.

 

splitDiagramIntoPages.png   



splitDiagramIntoPages_001.png


 

splitDiagramIntoPages_002.png


 


splitDiagramIntoPages_003.png


 

 

Previous                                                    Next                                                    Home

Force Layout in PlantUML Diagrams with Hidden Links

‘hidden’ links can be used to force the layout.

For example, without hidden links.

 

withoutHiddenLinks.txt

@startuml

together {
	interface Appendable
	interface Cloneable

        class System
}

together {
	interface Collection<E>
	interface List<E>

	class ArrayList<E> implements List
}

@enduml

 

Above snippet generate below diagram.

  


withHiddenLinks.txt

@startuml

together {
	interface Appendable
	interface Cloneable

        class System
}

together {
	interface Collection<E>
	interface List<E>

	class ArrayList<E> implements List
}

Appendable -[hidden]--> Collection
Cloneable -[hidden]--> ArrayList

@enduml

 

Above snippet generate below diagram.


 

 

 

Previous                                                    Next                                                    Home

How to Group Classes Together in PlantUML Diagrams?

Using ‘together’ keyword, we can group some classes together.

together.txt

@startuml

together {
	interface Appendable
	interface Cloneable

        class System
}

together {
	interface Collection<E>
	interface List<E>

	class ArrayList<E> implements List
}

Appendable -[hidden]--> Collection

@enduml

 

‘hidden’ links can be used to force the layout.

 

Above snippet generate below diagram.



 

 

Previous                                                    Next                                                    Home

Thursday, 31 August 2023

How to Use Color Gradients in PlantUML?

Color gradient shows a transition between two or more colors. You can use color gradient for background colors. Two color names are separated by following symbols.

 

a.   |

b.   /

c.    \

d.   -

 

colorGradient.txt

@startuml

skinparam backgroundcolor white/red
skinparam classBackgroundColor gold|CornflowerBlue

class Class1

class Class2 #red-green

note left of Class2 #LightBlue\9932CC
sample note
end note

package "java.util" #YellowGreen/DimGray {
	class Dummy
}

@enduml

 

Above snippet generate below diagram.

 


 

Previous                                                    Next                                                    Home

How to Customize the Color of Stereotyped Classes in PlantUML?

Using skinparam command, we can customize the color for stereotyped classes.

 

Example

skinparam class {
	BackgroundColor<<Animal>> #FFF0F0
         BorderColor<<Animal>> Red
}

 

stereoTypeClassColor.txt

@startuml

skinparam class {
	BackgroundColor LightBlue
	ArrowColor Tomato
	BorderColor SpringGreen
	BackgroundColor<<Animal>> #FFF0F0
    BorderColor<<Animal>> Red
}

abstract class Mammal

Class Dog <<Animal>>
class BullDog
class Poodle
class Beagle

class Cat  <<Animal>>
class PersianCat
class Ragdoll

Mammal <|-- Cat
Mammal <|-- Dog

Dog <|-- BullDog
Dog <|-- Poodle
Dog <|-- Beagle

Cat <|-- PersianCat
Cat <|-- Ragdoll

@enduml

 

Above snippet generate below diagram.

 


 

 

Previous                                                    Next                                                    Home