numpy

Pandas – Calculate Mean and Variance

Pandas – Calculate Mean and Variance Question: For a current project, I would like to calculate both the mean and variance for a group of values. My existing code calculates the mean through .agg(‘mean’). I tried to add , ‘var’ inside the bracket, which however yielded an error: f"numpy operations are not valid with " …

Total answers: 1

How to get the a percentage column along with the value/ count column

How to get the a percentage column along with the value/ count column Question: Shown below are the details from a DataFrame Below is the Syntax used to add a percentage column, df1 = df[[‘Attrition’, ‘Gender’,"JobSatisfaction"]] df1 = df1.groupby([‘Attrition’,’Gender’])[‘Job_Satisfaction’].value_counts().reset_index(name=’count’) df1[‘%’] = 100 * df1[‘count’]/ df1.groupby([‘Attrition’,’Gender’,’Job_Satisfaction’])[‘count’].transform(‘sum’) df1 = df1 .sort_values(by=[‘Gender’,’Attrition’,’Job_Satisfaction’]) df1 below is the results I …

Total answers: 2

Reset pandas cumsum when the condition is not satisified

Reset pandas cumsum when the condition is not satisified Question: I went through different stackoverflow questions and finally posting it because I couldnt solve one of the issues I am facing. I have a dataframe like below A B C group1 group1_c 12 group1 group1_c 12 group1 group1_c 12 group1 group1_c 1 group1 group1_c 12 …

Total answers: 1

Numba – TypingError with numpy symbol arrays (njit)

Numba – TypingError with numpy symbol arrays (njit) Question: I tried to execute this code, but when I call it for the first time to compile numba is angry on a line gen_code = np.array([]) : char_array = np.array([‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’, ‘s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’,’A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’, ‘K’,’L’,’M’,’N’,’O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’,’0′,’1′, ‘2’,’3′,’4′,’5′,’6′,’7′,’8′,’9′]) # creating a random 40-lenghted code from characters in char_array and returning as …

Total answers: 1

Coverter the type in the array

Coverter the type in the array Question: how to convert to the form like me in the example below: ex: [a, b, c,d ] >> [(a,b) (c,d)] [a,b,c,d] #wish to be like this [(a,b) (c,d)] Thanks Asked By: Quan Tran || Source Answers: Try this; l = [‘a’,’b’,’c’,’d’] n = 2 lst = [] for …

Total answers: 1

Allocate array with values and indices from other arrays

Allocate array with values and indices from other arrays Question: I have 2 arrays, one with values and the other with indices. Can I populate another array (with different dimensions) using these two in numpy? vals = [[0.7, 0.7, 0.2, 0.1], [0.3, 0.2, 0.1, 0.1]] indices = [[2, 5, 6, 8], [0, 1, 2, 7]] …

Total answers: 1

Pandas: filter for 2nd day of the month

Pandas: filter for 2nd day of the month Question: I have the following data. value date 2010-10-01 100 2010-10-02 200 2010-10-03 300 2012-10-02 100 2012-10-03 200 2012-10-04 300 2022-10-03 500 2022-10-04 600 I would like to filter for the 2nd day of the month for each year. So i should end up with date value …

Total answers: 1

Downsampling 3D array with numpy

Downsampling 3D array with numpy Question: Given array: A = array([[[1, 2, 3, 1], [4, 5, 6, 2], [7, 8, 9, 3]]]) I obtain the following array in the forward pass with a downsampling factor of k-1: k = 3 B = A[…,::k] #output array([[[1, 1], [4, 2], [7, 3]]]) In the backward pass I …

Total answers: 1

Group numbers into bins based on offset with Python

Group numbers into bins based on offset with Python Question: I have a list like this: ls = [0, 1, 2, 4, 6, 7] # it won’t have duplicates and it is sorted Now I want to group this list into bins based on a offset (in this example offset=1) which should return this: [[0, …

Total answers: 1