How to Change the Figure Size for a Seaborn Plot?

Seaborn is a comprehensive data visualization library used for the plotting of statistical graphs in Python. It provides fine-looking default styles and color schemes for making more attractive statistical plots. Seaborn is built on the top portion of the matplotlib library and is also integrated closely with data structures from pandas.                                                             How to change … Read more

3 Little-Known NumPy Tricks in One Line [Python Puzzle]

Trick #1: Slicing and Slice Assignment This one-liner demonstrates the power of three interesting NumPy features and how their combination can solve a small data science problem in a clean and efficient manner. Say, you are working at a company and the accountant asks you to analyze salary data of different employees in your company. … Read more

How to Print the Exception Without Exiting Your Python Program?

Problem Formulation Given a basic Python program. How to print an exception if it occurs without exiting the program? For example, consider the following program that raises a ZeroDivisionError: division by zero. The output is: You want the program to keep running and executing the print statement after giving you a note about the exception: … Read more

How to Get the Substring of a String in Python?

To get a substring of a given string in Python, you can use a popular feature called “slicing“. The syntax is string[start:stop:step] with the following meaning: start is the index of the first character included in the substring, stop is the index of the last character, but it is not itself included in the substring, … Read more

How to Select Multiple Columns in Pandas

The easiest way to select multiple columns in Pandas is to pass a list into the standard square-bracket indexing scheme. For example, the expression df[[‘Col_1’, ‘Col_4, ‘Col_7’]] would access columns ‘Col_1’, ‘Col_4’, and ‘Col_7’. This is the most flexible and concise way for only a couple of columns. To learn about the best 3 ways … Read more

Python – Inverse of Normal Cumulative Distribution Function (CDF)

Problem Formulation How to calculate the inverse of the normal cumulative distribution function (CDF) in Python? Method 1: scipy.stats.norm.ppf() In Excel, NORMSINV is the inverse of the CDF of the standard normal distribution. In Python’s SciPy library, the ppf() method of the scipy.stats.norm object is the percent point function, which is another name for the … Read more

Reverse a String in Python

There are four main ways to reverse a string in Python: Slicing s[::-1] with negative step size traverses the string from right to left. ”.join(reversed(s)) first creates an iterable of characters in reversed order, and then joins these characters to obtain the reversed string. A for loop using the range(len(s)-1, -1, -1) function traverses the … Read more

How to Print Subscript in Python?

Problem Formulation Given two strings x and y. Create a new string xy and print it to the shell. Consider the following examples: INPUT x = ‘hi’ y = ‘finxter’ OUTPUT: hifinxter INPUT x = ‘2’ y = ‘4’ OUTPUT: 24 INPUT x = ‘abc’ y = ‘[1, 2, 3]’ OUTPUT: abc[1, 2, 3] Solution … Read more

List of Unicode Superscript Characters

Character Description Character U+2070 SUPERSCRIPT ZERO ⁰ U+2071 SUPERSCRIPT LATIN SMALL LETTER I ⁱ U+2074 SUPERSCRIPT FOUR ⁴ U+2075 SUPERSCRIPT FIVE ⁡ U+2076 SUPERSCRIPT SIX ⁢ U+2077 SUPERSCRIPT SEVEN ⁷ U+2078 SUPERSCRIPT EIGHT ⁸ U+2079 SUPERSCRIPT NINE ⁹ U+207A SUPERSCRIPT PLUS SIGN ⁺ U+207B SUPERSCRIPT MINUS ⁻ U+207C SUPERSCRIPT EQUALS SIGN ⁼ U+207D SUPERSCRIPT LEFT … Read more