# Vectorized calculation of new timeseries in pandas dataframe

I have a pandas dataframe and I am trying to estimate a new timeseries V(t) based on the values of an existing timeseries B(t). I have written a minimal reproducible example to generate a sample dataframe as follows: import pandas as pd import numpy as np lenb = 5000 lenv = 200 l = 5… Read More Vectorized calculation of new timeseries in pandas dataframe

# Vectorization a code to make it faster than this

I have a little bit code which I’ll have to vectorizate it to make it faster. I’m not very attached into python and thinking that the for loop is not so efficient. Is there any way to reduce the time? import numpy as np import time start = time.time() N = 10000000 #9 seconds #N… Read More Vectorization a code to make it faster than this

# Vectorized way of checking a date column's calendar sequence

I have a dataframe which looks like this: Market Date Begin Date Settlement 0 2016-01-01 2016-01-01 26.1935 1 2016-01-01 2016-02-01 24.1071 2 2016-01-01 2016-03-01 21.0591 3 2016-01-01 2016-04-01 20.7348 4 2016-01-01 2016-05-01 20.2072 … … … … 265198 2022-09-21 2031-04-01 65.1300 265199 2022-09-21 2031-05-01 65.1300 265200 2022-09-21 2031-06-01 65.1300 265201 2022-09-21 2031-07-01 65.1300 265202 2022-09-21… Read More Vectorized way of checking a date column's calendar sequence

# Vectorization assign the newest value based on datetime

I have two dataframe. The first dataframe have only one column: email, the first dataframe is a complete list of email. The second dataframe is a dataframe with three column: email, subscribe_or_unsubscribe, date. The second dataframe is a history of user subcribing or unsubscribing from the email system. The second dataframe is sorted by date… Read More Vectorization assign the newest value based on datetime

# Vectorising a sum of scalar multiplied by a matrix, where the scaler is an element of a list

I’m trying to vectorise the following a = np.array([1,2]) b = np.array([[5,5],[5,5]]) target = 0 for _ in a: target = target + _ * b The above yields a 2×2 matrix where all entries are 15. How can I achieve this through vectorisation? I’ve been trying to cast a to be two 2×2 matrices,… Read More Vectorising a sum of scalar multiplied by a matrix, where the scaler is an element of a list

# Vectorizing a function with two inputs

I’m trying to vectorize the following functions: function f(x) z1 = 3*x z2 = x^2 return z1,z2 end function g(x,y) z = 2*x + 3*y*im return z end My goal, is to have one vector input as x to a function f(x), then take the result of the function g(x,y) and get a single vector… Read More Vectorizing a function with two inputs

# Optimizing results instead of apply; get df values and add to list of items

Simplifying my big problem into this I have the following datafarme: import pandas as pd df = pd.DataFrame({"letter":[‘A’,’B’,’D’,’E’,’G’,’W’,’G’,’M’,’E’,’Q’],’value’:[1,6,4,0,9,7,0,-1,5,3]}) and a list of items (name and value): items = [[‘John’,1],[‘Mike’,8],[‘Jessica’,4]] My goal is to add the letters in the df to the items such that if the value in the df + the value in the… Read More Optimizing results instead of apply; get df values and add to list of items

# Calculation of a ratio of columns to create a model

I have a set of data where i am trying to model the rate of TB cases per unit population. Am I correct in thinking to find the rate of TB per unit of the population is as simple as doing; rate <- tbData\$TB/tbData\$Population My df is called tbData with the following variables; head(TBdata) Indigenous… Read More Calculation of a ratio of columns to create a model

# Vectorized str.replace for multiple characters in pandas

I have a dataframe: {‘country’: {0: ‘Afghanistan?*’, 1: ‘Albania?*’}, ‘region’: {0: ‘Asia’, 1: ‘Europe’}, ‘subregion’: {0: ‘Southern Asia’, 1: ‘Southern Europe’}, ‘rate_per_1000’: {0: 6.7, 1: 2.1}, ‘count’: {0: ‘2,474’, 1: ’61’}, ‘year’: {0: 2018, 1: 2020}, ‘source’: {0: ‘NSO’, 1: ‘NSO’}} country region subregion rate_per_1000 count year source 0 Afghanistan?* Asia Southern Asia 6.7 2,474… Read More Vectorized str.replace for multiple characters in pandas