Compute Grouped Data Covariance in Python

Vani Nalliappan
Updated on 25-Feb-2021 05:26:51

393 Views

Assume, you have a dataframe and the result for calculating covariance from grouped data and corresponding column as, Grouped data covariance is:                   mark1       mark2 subjects maths    mark1    25.0    12.500000          mark2    12.5    108.333333 science  mark1    28.0    50.000000          mark2    50.0    233.333333 Grouped data covariance between two columns: subjects maths    12.5 science  50.0 dtype: float64SolutionTo solve this, we will follow the steps given below −Define a dataframeApply groupby function inside dataframe subjects ... Read More

Reshape DataFrame in Different Ways Using Python

Vani Nalliappan
Updated on 25-Feb-2021 05:24:37

476 Views

We can reshape a dataframe using melt(), stack(), unstack() and pivot() function.Solution 1Define a dataframe.Apply melt() function to convert wide dataframe column as rows. It is defined below, df.melt()ExampleLet’s see the below code to get a better understanding −import pandas as pd df = pd.DataFrame({'Id':[1, 2, 3], 'Age':[13, 14, 13], 'Mark':[80, 90, 85]}) print("Dataframe is:", df) print(df.melt())OutputDataframe is:  Id Age Mark 0 1 13   80 1 2 14   90 2 3 13   85 variable value 0    Id    1 1    Id    2 2    Id    3 3   Age    13 4   ... Read More

Sum Excluding One Element in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:38:37

750 Views

Suppose we have an array of Integers like this −const arr = [12, 1, 4, 8, 5];We are required to write a JavaScript function that takes in one such array as the only argument.The function should then return an array of exactly two integers −First integer should be the smallest possible sum of all the array elements excluding any one element.Second integer should be the greatest possible sum of all the array elements excluding any one element.The only condition for us is that we have to do this using one and only one for loop.For example −For the above array, ... Read More

Finding Desired Sum of Elements in an Array in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:35:34

281 Views

Suppose we have an array of Numbers like this −const arr = [1, 2, 1, 3, 2];We are required to write a JavaScript function that takes in one such array as the first argument. The second argument will be a number that represents a desired sum, let us call it sum, and the third and the last argument will also be a number that represents the count of numbers that should add up to the desired sum from the array (without repetition of elements), let's call this number num.The function should finally return the number of all such groups that ... Read More

Finding a Pair Divisible by a Number in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:29:07

328 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's call it arr and a single number as the second argument, let's call it num.The function should find all such pairs from the array where −arr[i] + arr[j] = num, and i < jFor example −If the input array and the number is −const arr = [1, 2, 3, 4, 5, 6]; const num = 4;Then the output should be −const output = [    [1, 3], [2, 6], [3, 5] ];ExampleThe code for this will be − Live Democonst arr ... Read More

Finding Matching Pair from an Array in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:22:32

2K+ Views

We are required to write a JavaScript function that takes in an array of Integers that might contain some repeating values. Our function should find out the number of pairs of identical integers we can extract out of the array.For example −If the input array is −const arr = [1, 5, 2, 1, 6, 2, 2, 9];Then the output should be −const output = 2;because the desired pairs are 1, 1 and 2, 2ExampleThe code for this will be − Live Democonst arr = [1, 5, 2, 1, 6, 2, 2, 9]; const countPairs = (arr = []) => {   ... Read More

Longest Subarray with Absolute Difference Equal to Some Number in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:20:56

179 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's say arr, and a number, let's say num, as the second argument. The function should find and return the length of the longest subarray (contiguous or non-contiguous) whose each pair has an absolute difference less than or equal to num.For example, if the input array and the number are −const arr = [7, 9, 8, 6, 6, 3]; const num = 1;Then the output should be −const output = 3, because the desired subarray is [7, 6, 6]ExampleThe code for ... Read More

Number of Digits that Divide the Complete Number in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:19:10

169 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number.For example −If the input number is −const num = 148;Then the output should be −const output = 2;because 148 is exactly divisible by 1 and 4 but not 8.ExampleThe code for this will be − Live Democonst num = 148; const countDividingDigits = (num = 1) => {    let count = 0;    const numStr = String(num);    for(let i = ... Read More

Longest String with Two Distinct Characters in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:16:39

394 Views

We are required to write a JavaScript function that takes in a string as the first argument and a number (smaller than the length of string) as the second argument. The function should delete characters from the original string and prepare a new string such that it's the longest string containing at most two distinct characters.Then at last the function should return the length of that desired string.For example: If the input string is −const str = 'kjeljsdl';Then the output should be −const output = 4;because the longest substring with at most 2 distinct characters is 'jljl'ExampleThe code for this ... Read More

Substring Combination in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:12:50

248 Views

We are required to write a JavaScript function that takes in two strings as the first and the second argument. Let's call these strings str1 and str2. The function should check whether there exists a substring combination in str2, that when combined yields str2.By substring combination, we mean that we can skip characters but we have to maintain the order of the characters selected from str1.For example −If the input strings are −const str1 = 'desxooajmepwele'; const str2 = 'example';Then the output should be −const output = true;because the string 'example' can be formed by picking some and maintianing the ... Read More

Advertisements