# Questions tagged [apply]

A function to call another function with a list of arguments.

**0**

votes

**0**answers

18 views

### ambiguous DataFrame in apply

I have a following code:
def joinOneRow(ser, df):
temp = df[df.index ==
max(df.index[df.index <=
ser.index[0]])]
return temp.values[0][0], temp.values[0]...

**0**

votes

**3**answers

42 views

### Empty strings in pandas series counted as one when getting the number of words in strings

I have a problem when counting the number of items in a pandas string series when there is no sting in a row.
I´m able to count the number of words when there are one ore more items per row. But, if ...

**0**

votes

**2**answers

29 views

### Merge data frame with conditions

below are my data frames.
df1<-data.frame(month=c("march", "april"), apple=c(1,NA), peach=c(10,NA))
df2<-data.frame(month=c("march", "april"), apple=c(5,3), peach=c(NA,NA))
I want R to do the ...

**0**

votes

**1**answer

33 views

### Pandas .apply() function not always being called in python 3

Hello I wanted to increment a global variable 'count' through a function which will be called on a pandas dataframe of length 1458.
I have read other answers where they talk about .apply() not being ...

**0**

votes

**1**answer

14 views

### Pandas apply tuple unpack function on multiple columns

Given a function that takes multiple arguments and returns multiple values as so:
def tuple_unpack(value, another_value):
''' does some interesting stuff ... '''
return value, another_value
Is ...

**2**

votes

**2**answers

39 views

### data frames: apply on two columns based on values

say i have a simple dataframe, for example:
import pandas as pd
data = {'letters' : ['a','b','c','d','e'], 'numbers' :[1,2,3,4,5]}
df = pd.DataFrame(data)
the table looks like this
simple ...

**1**

vote

**0**answers

37 views

### Speed up matrix operations

I have two matrices of the same dimensions (which in reality are of 2,500 x 15,000):
set.seed(1)
a.mat <- matrix(rnorm(25*150),nrow=25,ncol=150,dimnames=list(paste0("p",1:25),paste0("c",1:150)))
b....

**0**

votes

**2**answers

48 views

### Vectorising pandas dataframe apply function for user defined function in python

I want to compute week of the month for a specified date. For computing week of the month, I currently use the user-defined function.
Input data frame:
Output data frame:
Here is what I have tried:
...

**2**

votes

**2**answers

62 views

### Improve Performance of Apply Method

I would like to groupby by the variable of my df "cod_id" and then apply this function:
[df.loc[df['dt_op'].between(d, d + pd.Timedelta(days = 7)), 'quantity'].sum() \
for d ...

**1**

vote

**2**answers

21 views

### Apply function to two columns Pandas

I'm trying to apply a function that calculates the R2 score to two columns of a pandas data frame. It works if first I group by some other column but when applied to the whole dataframe it fails.
...

**1**

vote

**1**answer

18 views

### Applying a function to a dataframe column pandas

I currently created a function to apply to a pandas dataframe, as below:
def timebin (col):
timebin = []
for x in range(len(col)):
if x>=0 & x<5:
return 'night'
elif x>5 &...

**1**

vote

**2**answers

35 views

### Using rep inside sapply to strech a vector according to another vector

I want to generate a data.frame of edges. Problems arise when many edges end on one node. Edges are defined in vectors from and to.
# Data
vertices <- data.frame(id = 1:3, label = c("a", "b", "c"),...

**2**

votes

**1**answer

55 views

### Groupby and apply a function

I would like to groupby by the variable of my df "cod_id" and then apply this function:
[dd.loc[dd['dt_op'].between(d, d + pd.Timedelta(days = 7)), 'quantity'].sum() \
for d ...

**0**

votes

**1**answer

22 views

### R: Apply over every column of nXm matrix, return n*n*m array

What is the appropriate function to apply a function that returns an n*n matrix, over every column of a matrix/nXm array, and get back an nXnXm array? I can get it to work with a list:
dm <- ...

**1**

vote

**2**answers

28 views

### Conditionally append dataframes to specific levels of a nested list

I have a nested list (mylist), and I would like to cbind a dataframe (colors) automatically to a lower level list (iris) only if the name of the list contains a specific string (iris), but I'm running ...

**0**

votes

**0**answers

31 views

### Pandas apply function to subset of rows

I am setting a column of a pandas dataframe to all zeros initially, and then using apply to populate a subset of this column. However, after attempting to do so, all values in the column still remain ...

**2**

votes

**1**answer

27 views

### How to use “apply” with a dataframe and avoid SettingWithCopyWarning?

I am using the following function with a DataFrame:
df['error_code'] = df.apply(lambda row: replace_semi_colon(row), axis=1)
The embedded function is:
def replace_semi_colon(row):
errrcd = str(...

**0**

votes

**0**answers

7 views

### Using an alias to create another alias in SQL

I am using Apex to write SQL queries. I have a Products Table which displays the product name and product price. I create an alias named product tax that multiplies product price by .07:
SELECT ...

**0**

votes

**1**answer

60 views

### Convert my simple 'for loop' into an apply function

I'm trying to embrace the apply family more, but still having trouble. I mostly understand simple cases with lapply (as below), but am having issues with more complicated apply functions.
I have a ...

**0**

votes

**2**answers

41 views

### Coding events that will happen within n periods for Logistic Regression. In pandas

I have a pandas data-frame with millions of accounts. The data-frame has a column titled "eventHappens" that equals 1 in the period in which an event happens. Here is an example:
import pandas as ...

**0**

votes

**1**answer

75 views

### Create new columns by applying a function on selected rows pandas

I would like to use the following function on a large dataset, 300000 rows with 1500 unique ID. And create two new columns as in below. The function below works when applied to the data frame as a ...

**2**

votes

**2**answers

27 views

### Pandas filldown by other column (I don't know how to name this)

I have a pandas data-frame with a column that indicates if the terms of an account were changed during a particular period with a value of "Y".
Here is an example:
import pandas as pd
account = [1, 1,...

**0**

votes

**1**answer

25 views

### R: filter() in for loop running through columns

in R - trying to loop through a list of column names, filtering by a specific entry and counting the number of occurrences of that entry. I'm trying to run something like what's below in a pipe within ...

**0**

votes

**1**answer

38 views

### Replacing for loop with apply

I have a simple for-loop for pattern matching and and getting the values from another matrix. It is bit slow to run for large number of rows. I am trying to convert it into a function and then using ...

**0**

votes

**2**answers

29 views

### summary coloum with conditional if in r

I want to have summary coloum with this conditions:
If part 1 greater than part 2 & 3 than give value 1
If part 2 greater than part 1&3 than give value -1
If part 3 greater than part 1&2 ...

**0**

votes

**1**answer

28 views

### how to apply functions to a series of dataframe filtered by categorical variables in R

I have a data frame df containing 3 numerical variables,1 outcome and 1 categorical variable
I need to carry out a procedure which involves filtering the df by different levels of category A or B and ...

**-2**

votes

**0**answers

26 views

### Get data from another dataframe using apply function in R [duplicate]

I have two data frames like below:
Long
ID STATUS CREATETIME
1 AUTO-CLOSED 1538919900835
1 CLOSED 1538919900835
1 CONFIRMED 1538919900835
2 TRACKING 1538919900837
2 AUTO-...

**0**

votes

**0**answers

24 views

### Zoo Object Values Lost At Certain Index Value

I've run into a very odd problem working with FRED data via the quantmod package. I'm downloading SP500 time series from the site and converting it to quarterly, year-over-year changes in the index ...

**1**

vote

**1**answer

18 views

### compare rows of data frame to another

I have two data frames, one is from experiment (df1) and another is reference (df2). They are semi-quantitative values from specimens, from 1 to 50. I would like to compare each rows of df1 from ...

**1**

vote

**0**answers

19 views

### pandas groupby apply optimizing a loop

For the following data:
index bond stock investor_bond inverstor_stock
0 1 2 A B
1 ...

**0**

votes

**1**answer

33 views

### Count number of rows based on 3 unique conditions with many (over 50) possible combinations

I have some data where each row represents one flower. I'm trying to count the number of rows in the dataframe based on 3 variables. Here is a sample of what the data looks like.
ï..petal_size ...

**0**

votes

**0**answers

43 views

### How to properly use pandas groupby with apply function for side effects? (First group applied twice)

I am using pandas to groupby certain columns in a dataframe and apply a custom function to these groups. The applied function makes use of side effects and acts on global data objects within the ...

**0**

votes

**1**answer

28 views

### r row multiplication and row sum

I am dealing with a large dataset and the condensed version of this dataset is as shown below
Col1 Z1 Z2 Z3 Z4 Z5 Z6
0.077 1.6163062 ...

**1**

vote

**1**answer

48 views

### How should I vectorize this simple for loop in R?

The loop to be vectorized:
n = 1000000
x = numeric(n)
for (I in 1:n) x[i] = rpois(1, 3) + rpois(1, 5)
The original is just too slow, I don't have much experience with for loops or the apply ...

**-4**

votes

**1**answer

35 views

### transform data set so that each record represent unique place rather than multiple records for same place using r

I have a dataset with following records:
ID type description name colony zip
1 officer1 general sam maria 20909
1 officer2 general phil maria 20909
1 officer3 general horton ...

**0**

votes

**1**answer

30 views

### How can I use multiple apply with pandas

I'm using pandas with Python to export Excel to JSON. It's my first day with this lib and with python :)
Excel file :
My excel datas
or DataFrame :
DATA1 NAME CLIENT PHONE CLIENT NAME BOSS ...

**2**

votes

**3**answers

40 views

### R_Extract the row and column of the element in use when using apply function

How to extract the row and column of the element in use when using apply function? For example, say I want to apply a function for each element of the matrix where row and column number of the ...

**0**

votes

**2**answers

27 views

### R Dplyr and string values, how to split and get the second element? vapply/sapply

Been having difficulty with this one data frame manipulation in R.
I have two columns for well height and a date-time string ("yyyy-mm-dd HH:MM:ss").
I would like to extract all the rows from this ...

**1**

vote

**2**answers

32 views

### Call function many times without duplicate it (with %>%)

Let's assume that we have 3 functions with this minimal functionality:
func1 <- function (x) {
x + 1
}
func2 <- function (x, plus = T) {
if (plus == TRUE) {
x + 2
} else {
x - 5
...

**0**

votes

**1**answer

17 views

### R apply breaks for non-numeric parameters

I lack a good understanding of the apply function which is possibly leading to an error message while looping over a dataframe where one column is a character.
I am using apply by passing rows of a ...

**0**

votes

**1**answer

30 views

### Replace for-loop by function

I have the following for-loop, which creates a dummy variable for each level in the Code variable, which I want to write as a function so I can use it in an apply-function:
for(level in data$Letters)...

**3**

votes

**2**answers

87 views

### Batch Forecasting; using apply() function instead of for loop. apply() function gives different point forecast

So far I was using this method from professor Hyndman when I had multiple time series to forecast. But when I have a large number of ts it is fairly slow.
Now I am trying to use apply() function as ...

**0**

votes

**1**answer

65 views

### Scala: Typed method that returns object of the provided Type

Good day everyone,
I need to create a Typed Class with a Typed function to parse an Array[String] and returns an Array[OutputType]
class Data()
class IntData(var element: Int) extends Data
class ...

**2**

votes

**2**answers

47 views

### Creating New Column Using IF and Apply Function

My Age data is float and I want to create a new categorical column with three values: Elder, Child and Adult. I defined a function and added iloc[0] to iterate individual values in existing df['Age'] ...

**0**

votes

**1**answer

12 views

### Pandas - flatten the diagonal values of dataframe returned from an apply loc operation

I have two dataframes -- for each row in df I want to look up the matching epoch_minute - lag and grab the corresponding average_hc value.
>>> df.head()
epoch_minute headcount
0 ...

**0**

votes

**2**answers

52 views

### Counting the elements in rows and map to column in r

I would like summarize my data by counting the entities and create counting_column for each entity.
let say:
df:
id class
1 A
1 B
1 A
1 A
1 B
1 c
2 A
2 B
2 B
2 ...

**1**

vote

**1**answer

24 views

### Assign value matrix based on index condition

How can I assign a value into a matrix based in a vector condition index. A working example is:
# Input:
r <- c(2, 1, 3)
m <- matrix(rep(0, 9), nrow = 3)
# Desired output
result <- matrix(...

**0**

votes

**3**answers

27 views

### Create new columns in pandas dataframe using apply

I am looking to create new columns in a pandas dataframe based on other column value using apply. I receive this error and I don't understand why:
File "C:\dev\Anaconda3\lib\site-packages\pandas\...

**-2**

votes

**1**answer

35 views

### How to apply a function from a package to a dataframe

How can I apply a package function to a data frame ?
I have a data set (df) with two columns (total and n) on which I would like to apply the pois.exact function (pois.exact(x, pt = 1, conf.level = 0....

**1**

vote

**1**answer

54 views

### Replacing Inf Values with 0 Given a List of Columns in R

I'm terrible at apply functions and every answer I looked up on here somehow is hard for me to apply to this problem, I've tried as hard as I've can to not post here.
I have a list of column names ...