Found 26504 Articles for Server Side Programming

Python - Cumulative Mean of Dictionary keys

AmitDiwan
Updated on 20-Sep-2021 11:22:02

298 Views

When it is required to find the cumulative mean of the dictionary keys, an empty dictionary is created, and the original dictionary is iterated over, and the items are accessed. If this is present in the dictionary, the key is appended to the empty dictionary, otherwise the value is placed instead of the key.ExampleBelow is a demonstration of the samefrom statistics import mean my_list = [{'hi' : 24, 'there' : 81, 'how' : 11}, {'hi' : 16, 'how' : 78, 'doing' : 63}] print("The list is : ") print(my_list) my_result = dict() for sub ... Read More

Python - How to fill NAN values with mean in Pandas?

AmitDiwan
Updated on 20-Sep-2021 11:21:47

5K+ Views

For mean, use the mean() function. Calculate the mean for the column with NaN and use the fillna() to fill the NaN values with the mean.Let us first import the required libraries −import pandas as pd import numpy as npCreate a DataFrame with 2 columns and some NaN values. We have entered these NaN values using numpy np.NaN −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'], "Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } )Finding mean of the column values with NaN i.e, for Units columns here. So, the Units ... Read More

Python - Given a list of integers that represents a decimal value, increment the last element by 1

AmitDiwan
Updated on 20-Sep-2021 11:15:57

480 Views

When it is required to increment the last element by 1 when a decimal value is given an input, a method named ‘increment_num’ is defined that checks to see if the last element in the list is less than 9. Depending on this, operations are performed on the elements of the list.ExampleBelow is a demonstration of the samedef increment_num(my_list, n): i = n if(my_list[i] < 9): my_list[i] = my_list[i] + 1 return my_list[i] = 0 ... Read More

Python - Given an integer 'n', check if it is a power of 3, and return True, otherwise False.

AmitDiwan
Updated on 20-Sep-2021 11:13:54

736 Views

When it is required to check if a given variable is of power 3, a method named ‘check_power_of_3’ is defined that takes an integer as parameter. The modulus operator and the ‘//’ operator is used to check for the same and return True or False depending on the output.ExampleBelow is a demonstration of the samedef check_power_of_3(my_val): if (my_val == 0): return False while (my_val != 1): if (my_val % 3 != 0): return ... Read More

Python - Given an integer 'n', check if it is a power of 4, and return True, otherwise False.

AmitDiwan
Updated on 20-Sep-2021 11:11:57

834 Views

When it is required to check if a given variable is of power 4, a method named ‘check_power_of_4’ is defined that takes an integer as parameter. The modulus operator and the ‘//’ operator is used to check for the same and return True or False depending on the output.ExampleBelow is a demonstration of the samedef check_power_of_4(my_val): if (my_val == 0): return False while (my_val != 1): if (my_val % 4 != 0): return ... Read More

Python - Merge Pandas DataFrame with Left Outer Join

AmitDiwan
Updated on 20-Sep-2021 11:14:50

2K+ Views

To merge Pandas DataFrame, use the merge() function. The left outer join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “left”At first, let us import the pandas library with an alias −import pandas as pd Let’s create two DataFrames to be merged −# Create DataFrame1 dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90]    } ) # Create DataFrame2 dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], ... Read More

Python - Check if two strings are isomorphic in nature

AmitDiwan
Updated on 20-Sep-2021 11:08:09

370 Views

When it is required to check if two strings are isomorphic in nature, a method is defined that takes two strings as parameters. It iterates through the length of the string, and converts a character into an integer using the ‘ord’ method.ExampleBelow is a demonstration of the sameMAX_CHARS = 256 def check_isomorphic(str_1, str_2):    len_1 = len(str_1)    len_2 = len(str_2)    if len_1 != len_2:       return False    marked = [False] * MAX_CHARS    map = [-1] * MAX_CHARS    for i in range(len_2):       if map[ord(str_1[i])] == -1: ... Read More

Python - Check if a number and its double exists in an array

AmitDiwan
Updated on 20-Sep-2021 11:03:26

565 Views

When it is required to check if a number and its double exists in an array, it is iterated over, and multiple with 2 and checked.ExampleBelow is a demonstration of the samedef check_double_exists(my_list): for i in range(len(my_list)): for j in (my_list[:i]+my_list[i+1:]): if 2*my_list[i] == j: print("The double exists") my_list = [67, 34, 89, 67, 90, 17, 23] print("The list is :") print(my_list) check_double_exists(my_list)OutputThe list is : [67, 34, 89, ... Read More

Python Pandas – Remove numbers from string in a DataFrame column

AmitDiwan
Updated on 20-Sep-2021 11:07:42

3K+ Views

To remove numbers from string, we can use replace() method and simply replace. Let us first import the require library −import pandas as pdCreate DataFrame with student records. The Id column is having string with numbers −dataFrame = pd.DataFrame(    {       "Id": ['S01', 'S02', 'S03', 'S04', 'S05', 'S06', 'S07'], "Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } )Remove number from strings of a specific column i.e. “Id” here −dataFrame['Id'] = dataFrame['Id'].str.replace('\d+', '') ExampleFollowing is the code −import pandas as pd # Create DataFrame with ... Read More

Python - Find the number of prime numbers within a given range of numbers

AmitDiwan
Updated on 20-Sep-2021 11:01:52

2K+ Views

When it is required to find the prime numbers within a given range of numbers, the range is entered and it is iterated over. The ‘%’ modulus operator is used to find the prime numbers.ExampleBelow is a demonstration of the samelower_range = 670 upper_range = 699 print("The lower and upper range are :") print(lower_range, upper_range) print("The prime numbers between", lower_range, "and", upper_range, "are:") for num in range(lower_range, upper_range + 1): if num > 1: for i in range(2, num): if (num % ... Read More

Advertisements