Pandas.DataFrame.filter() Method in Python

Pandas Dataframe filter() method is “used to filter the DataFame rows and columns.” The returned DataFrame contains only rows and columns specified by the function. Syntax DataFrame.filter(self: ~FrameOrSeries, items=None, like: Union[str, NoneType] = None, regex: Union[str, NoneType] = None, axis=None) Parameters items: list-like Keep labels from the axis that are in items. like: str Keep labels … Read more

Pandas DataFrame assign() Method

Pandas DataFrame assign() method is “used to assign new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original ones.” Syntax DataFrame.assign(**kwargs) Parameters **kwargs: dict of {str: callable or Series} The column names are keywords. If the values are callable, they are computed on the DataFrame and … Read more

What is the numpy.delete() Method

Numpy.delete() method “returns a new array with the deletion of sub-arrays along with the mentioned axis.” Syntax numpy.delete(array, object, axis = None) Parameters The np.delete() function takes three parameters: array: This is the input array. object: This can be any single number or a subarray. axis: This indicates the axis to be deleted from the … Read more

Python math.gamma() Method

The math.gamma() method is used to calculate the gamma function of a number. For positive integers, gamma(num) is equivalent to factorial (num-1)!. Syntax import math math.gamma(num) Parameter num(required): It is a numeric value. Return Value It returns the gamma value of the number as a float. If num is an either zero(0) or negative integer , it … Read more

Python math.erfc() Method

The math.erfc() method is used to return complementary error function of a given number. Syntax import math math.erfc(num) Parameter num(required): A numeric value, which can range between – inf and + inf. Return Value Returns a float value between 0 and 2, representing the complementary error function of num It throws TypeError if the num is not … Read more

Python math.erf() Method

The math.erf() method is used to return the error function of a given number. This method is particularly useful in statistics for computing probabilities and in error analysis. Syntax import math math.erf(num) Parameter num(required): A numeric value, which can range between – inf and + inf. Return Value Returns a float value between -1 and 1, representing … Read more

Python math.radians() Method

The math.radians() method is used to convert a degree value into radians. The conversion formula is radians = degrees * (π / 180). Syntax math.radians(degrees) Parameter degrees(required): An angle in degrees that you want to convert to radians. Return value It returns the value of an angle in radians as a float. If the input … Read more

Python math.tanh() Method

The math.tanh() method returns the hyperbolic tangent of a given number. Syntax import math math.tanh(num) Parameters num(required): A number for which you want to calculate the hyperbolic tangent. Return Value Returns a float value ranging from -1 to 1, representing hyperbolic tangent of  a number. If the num is not a number, it returns TypeError. Visual … Read more

Python math.sinh() Method

The math.sinh() method is used to return the hyperbolic sine of a given number. Syntax import math math.sinh(num) Parameter num(required): A number for which you want to calculate the hyperbolic sine. Return Value Returns a hyperbolic sine value of the number in the float datatype. If the num is not a number, it raises TypeError. Visual Representation Example … Read more

Python math.cosh() Method

The math.cosh() method is used to return the hyperbolic cosine of a given number. Syntax import math math.cosh(num) Parameter num(required): It is an input number. Return value It returns hyperbolic cosine of the number as a float. If the num is not a number, it returns TypeError. Visual Representation Example 1: How to Use math.cosh() Method import … Read more

Python math.degrees() Method

The math.degrees() method is used to convert an angle from radians to degrees. Syntax import math math.degrees(num) Parameters num(required): A number representing an angle in radians that you want to convert to degrees. Return Value It returns the degree value of the num in the float datatype. If the num is not a number, it returns TypeError. Visual … Read more

Python Math.hypot() Method

The math.hypot() method is used to return Euclidean norm. Syntax math.hypot(a1,a2,…an) Parameters This method can also handle more than two arguments in Python 3.8 and later a1, a2,…an(required): The coordinates of the point. Return Value It returns a float value, which is the calculated Euclidean norm. If passing value is not the number, it returns … Read more

Python math.atan() Method

The math.atan() method is used to calculate the arc tangent of a given number. Syntax import math math.atan(num) Parameter num(required): It is a number. Return Value It returns the arc tangent of the number as a float, with values ranging from from -PI/2 to PI/2. If the num is not a number, it raises TypeError. Visual Representation Example … Read more

Python math.asin() Method

The math.asin() method is used to return the arc sine(inverse sine) of a given number. Syntax import math math.asin(num) Parameter num(required): A number for which you want to calculate the arc sine. The value of num must be in the range -1 to 1. Return Value It returns the arc sine value of the number … Read more

Python math.exp() Method

The math.exp() is used to calculate the exponential of a given number, specifically the value of e raised to the power of that number. The value of e is approximately equal to 2.71828. Syntax import math math.exp(num) Parameter num(required): The exponent to which e is raised. Return Value Returns a float value representing e raised to … Read more

Python math.trunc() Method

The math.trunc() method removes the fractional part of a number without rounding it. It simply truncates off the decimal portion, regardless of whether the decimal part is greater or less than 0.5. Syntax import math math.trunc(num) Parameter num(required): A number that you want to truncate. Return value It returns the truncated integer part of num. … Read more

How to Use if, else and elif in Python Lambda Functions

In Python, lambda functions are a way to create small anonymous functions (functions without a name). They are often used for simple operations and can contain only a single expression. However, you can simulate the behavior of `if`, `else`, and `elif` conditions within a lambda function using conditional expressions (ternary operators). Using if and else … Read more

Python math.isinf() Method

The math.isinf() method is used to check whether a number is infinite(either positive or negative infinity). Syntax import math math.isinf(num) Parameter num(required): The value to be checked. Here num can be an integer, float, double, finite inf, NaN, etc. Return Value It returns True if the given number is an infinite, otherwise False. Visual Representation Example 1: How to Use math.isinf() … Read more

How to Print List of Files in Directory and Subdirectories in Python

Here are the five ways to print a list of files in a directory and subdirectories in Python: Using os.walk() Using pathlib module Using os.listdir() Using os.scandir() Using glob.glob() Here is the directory “dir”, whose files we will print: Method 1: Using os.walk() The os.walk() function is a generator that yields a tuple of (dirpath, … Read more

Python Tuple count() Method (With Examples)

Python tuple count() method  “returns the number of times the specified element appears in the tuple.” Syntax tuple.count(element) Parameters “element”: It is an element to count in the tuple. Return Value Python tuple count() method returns the number of occurrences of one element in the tuple. Example 1: How to Use tuple.count() Method # Declaring … Read more

How to Access Characters in String by Index in Python

Here are three ways to access characters in String by index in Python: Using Square Brackets[] Negative Indexing Index Range Method 1: Using Square Brackets[] String indexing in Python is zero-based: the first character in the string has index 0, the next has index 1, and so on. Visual Representation Example text = “Amazon” print(text[0]) … Read more

How to Convert(Save) Python Dictionary to CSV File

To convert a dictionary to csv in Python, you can use the csv.DictWriter() method. Here is the step-by-step guide: Step 1: Import CSV module import csv Step 2: Define the data tech_companies_data = [ {“Company”: “Apple”, “Founded”: 1976, “Headquarters”: “Cupertino, California, USA”}, {“Company”: “Microsoft”, “Founded”: 1975, “Headquarters”: “Redmond, Washington, USA”}, {“Company”: “Google”, “Founded”: 1998, “Headquarters”: “Mountain … Read more

Python String center() Method

Python String center() method returns a new string, centered by padding it with the specified character(default is space) until it reaches a given width. Syntax string.center(width, fillchar) Parameters width(required): length of the string with padded characters. fillchar(optional):  The character to fill the padding. The default is a space. Return value It returns a new string … Read more

Python any() Function

Python any() function “returns True if any element of an iterable is True. If not, it returns False.” Syntax any(iterable) Parameters iterable: The any() function takes an iterable (list, string, dictionary, etc.) in Python. Return value The any() function returns the following values: True if at least one item of an iterable is True. False … Read more

Python hash() Method

Python hash() method returns the hash value of an immutable object. It works only with immutable objects. This means you can get the hash of an integer, float, string, tuple, etc., but not of lists, dictionaries, or other mutable types. The hash value is an integer used to quickly compare dictionary keys during a dictionary lookup. … Read more

Python Dictionary get() Method

Python Dictionary get() method “returns the value of the specified key if the key is in the dictionary, otherwise it returns None (or a specified default value).“ Syntax dictionary.get(keyname, value) Parameters The keyname name parameter and the keyname of the item you want to return the value are required. The value parameter is Optional. It … Read more

Python String count() Method

Python String count() method is used to count how many times a specified value (substring) appears in a string. Syntax string.count(substring, start, end) Parameters substring(required): It is the substring to search for.  start(optional): It is the starting index within the string where the search starts(Default 0).  end(optional): It is the ending index within the string … Read more

Pandas Series value_counts() Method

Pandas Series value_counts() method “returns a Series containing counts of unique values.” The resulting object will be in descending order, so the first element is the most frequently-occurring element. It excludes NA values by default. Syntax Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True) Parameters normalize: boolean, default False. If True, the object returned will contain the relative … Read more