Saturday, 26 August 2023

How to Get the Name of a Pandas Series

Using ‘name’ attribute, we can get the name of the series.

 

Example

primes_series = pd.Series(primes, index=primes_index, name='Hold the Prime numbers')

 

Above snippet sets the name to 'Hold the Prime numbers'. We can access the same with ‘primes_series.name’.

 

series_name.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')

print('primes_series name : ', primes_series.name)

 

Output

primes_series name :  Hold the Prime numbers

 

Previous                                                 Next                                                 Home

How to Get the Index Labels of a Pandas Series?

Using series ‘index’ attribute, we can get the index labels of the series.

 

Example 1: If you do not specify the index explicitly, then a numeric index is used by default.

even_numbers = [2, 4, 6, 8]
even_numbers_series = pd.Series(even_numbers)

‘even_numbers_series.index’ return RangeIndex(start=0, stop=4, step=1)

 

Example 2: Explicitly specifying the index.

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

'primes_series.index' return Index(['first_prime', 'second_prime', 'third_prime'], dtype='object')

 

Find the below working application.

 

get_series_index.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)

print('primes_series')
print(primes_series, '\n')
print('index_labels of primes_series is ', primes_series.index)

even_numbers = [2, 4, 6, 8]
even_numbers_series = pd.Series(even_numbers)
print('\neven_numbers_series')
print(even_numbers_series, '\n')
print('index_labels of even_numbers_series is ', even_numbers_series.index)

Output

primes_series
first_prime     2
second_prime    3
third_prime     5
dtype: int64 

index_labels of primes_series is  Index(['first_prime', 'second_prime', 'third_prime'], dtype='object')

even_numbers_series
0    2
1    4
2    6
3    8
dtype: int64 

index_labels of even_numbers_series is  RangeIndex(start=0, stop=4, step=1)

 

Previous                                                 Next                                                 Home

Tuesday, 22 August 2023

How to Check for Unique Values in Pandas

Series ‘is_unique’ attribute return True if the series contain unique elements, else False.

 

unique_elements_check.py

import pandas as pd

series1 = pd.Series([2, 3, 5])
series2 = pd.Series([2, 3, 5, 2])

print('is series1 contain duplicates ? ', series1.is_unique)
print('is series2 contain duplicates ? ', series2.is_unique)

 

Output

is series1 contain duplicates ?  True
is series2 contain duplicates ?  False

 

 

Previous                                                 Next                                                 Home

How to Add a Label to a Line in Pyplot?

Step 1: Use label argument of plot method to specify the line label.

plt.plot(x1, y1, color='red', label='red-line')

 

Step 2: Use legend method to explain various elements of the plot. ‘legend’ method internally takes all the labels, all the  line objects as input and creates a legend that shows the labels next to the corresponding lines in the plot.

 

add_label_to_line.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25]
y1 = [20, 40, 60, 80, 100]

x2 = [10, 20, 30, 40, 50]
y2 = [30, 60, 90, 120, 150]

# Red color line
plt.plot(x1, y1, color='red', label='red-line')

# Green color line
plt.plot(x2, y2, color='g', label='green-line')

# Ti display the legend on the plot
plt.legend()

plt.show()

 

Output




Previous                                                    Next                                                    Home

How to Define Association Classes in PlantUML

Association class is part of an association relationship between two other classes.

 

Example

(Person, Address) .. PersonAddress

 

associationClass.txt

@startuml

class Person{
	int id
	String name
}

class Address{
	int id
	String country
	String state
	String city
	String pin
}

class PersonAddress{
	int personId
	int addressId
}

Person "0..*" - "1..*" Address
(Person, Address) .. PersonAddress

@enduml

 

Above snippet generate below diagram.

 

 


You can define the association in another direction using below statement (two dashes between classes).

 

Person "0..*" -- "1..*" Address

 

associationClass2.txt

@startuml
class Person{
	int id
	String name
}

class Address{
	int id
	String country
	String state
	String city
	String pin
}

class PersonAddress{
	int personId
	int addressId
}

Person "0..*" -- "1..*" Address
(Person, Address) .. PersonAddress
@enduml

Above snippet generate below diagram.



 

 

Previous                                                    Next                                                    Home

Monday, 21 August 2023

How to Customize Arrow Direction in PlantUML Class Diagrams?

You can customize the arrow direction using left, right, up, down keywords.

arrowDirection.txt

@startuml

Human -left-> Brain
Human -right-> Lungs
Human -up-> Liver
Human -down-> Heart

@enduml

Above snippet generate below diagram.

 


By default, PlantUML build the diagrams from top to bottom, let’s changes this alignment to ‘left to right’ and see what happens.

 

leftToRightAlignment.txt

@startuml

left to right direction

Human -left-> Brain
Human -right-> Lungs
Human -up-> Liver
Human -down-> Heart

@enduml

Above snippet generate below diagram.




  

Previous                                                    Next                                                    Home

How to Change the Line Color in a Pyplot Line Graph?

By setting the ‘color’ argument, we can specify the color of the lines. Following table summarizes the different formats that we can specify a value to the color argument.

 

Format

Description

Color names

Color names like 'red', 'green', 'blue', 'black', 'white', 'purple', 'orange' can be specified directly. Reference: https://matplotlib.org/2.0.2/api/colors_api.html

Color abbreviations

Single character abbreviations are supported. For example, 'r' for red, 'g' for green, 'b' for blue, 'k' for black, 'w' for white, etc.

Hexadecimal notation

Color can be specified using a six-digit hexadecimal representation of colors. For example, '#FF0000' represents red, '#00FF00' represents green, and '#0000FF' represents blue.

RGB notation

Color can be specified using RGB tuple. For example, (1, 0, 0) represents red, (0, 1, 0) represents green, and (0, 0, 1) represents blue.

RGBA notation

Similar to RGB, but it takes an addition value to represent opacity rangin from 0 to 1.

 

Examples

# Red color line
plt.plot(x1, y1, color='red', label='red-line')

# Green color line
plt.plot(x2, y2, color='g', label='green-line')

# Blue color line
plt.plot(x3, y3, color='#0000FF', label='blue-line')

# Aqua color line
plt.plot(x4, y4, color=(0, 1, 1), label='aqua-line')

# Red color line with opacity 0.5
plt.plot(x5, y5, color=(1, 0, 0, 0.5), label='red-line-opacity')

 Find the below working application.

line_color.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25]
y1 = [20, 40, 60, 80, 100]

x2 = [10, 20, 30, 40, 50]
y2 = [30, 60, 90, 120, 150]

x3 = [15, 30, 45, 60, 75]
y3 = [8, 16, 24, 32, 40]

x4 = [12, 24, 36, 48, 60]
y4 = [15, 30, 45, 60, 75]

x5 = [8, 16, 24, 32, 40]
y5 = [25, 50, 75, 100, 125]

# Red color line
plt.plot(x1, y1, color='red', label='red-line')

# Green color line
plt.plot(x2, y2, color='g', label='green-line')

# Blue color line
plt.plot(x3, y3, color='#0000FF', label='blue-line')

# Aqua color line
plt.plot(x4, y4, color=(0, 1, 1), label='aqua-line')

# Red color line with opacity 0.5
plt.plot(x5, y5, color=(1, 0, 0, 0.5), label='red-line-opacity')

# Ti display the legend on the plot
plt.legend()

plt.show()

 

Output

 


 

  

Previous                                                    Next                                                    Home