apply

apply a function to each row of the dataframe

余生颓废 提交于 2020-01-24 03:04:27
问题 What is a more elegant way of implementing below? I want to apply a function: my_function to a dataframe where each row of the dataframe contains the parameters of the function. Then I want to write the output of the function back to the dataframe row. results = pd.DataFrame() for row in input_panel.iterrows(): (index, row_contents) = row row_contents['target'] = my_function(*list(row_contents)) results = pd.concat([results, row_contents]) 回答1: We'll iterate through the values and build a

apply a function to each row of the dataframe

喜欢而已 提交于 2020-01-24 03:03:18
问题 What is a more elegant way of implementing below? I want to apply a function: my_function to a dataframe where each row of the dataframe contains the parameters of the function. Then I want to write the output of the function back to the dataframe row. results = pd.DataFrame() for row in input_panel.iterrows(): (index, row_contents) = row row_contents['target'] = my_function(*list(row_contents)) results = pd.concat([results, row_contents]) 回答1: We'll iterate through the values and build a

R: Why am I not getting type or class “factor” after converting columns to factor?

心已入冬 提交于 2020-01-23 13:37:28
问题 I have the following setup. df <- data.frame(aa = rnorm(1000), bb = rnorm(1000)) apply(df, 2, typeof) # aa bb #"double" "double" apply(df, 2, class) # aa bb #"numeric" "numeric" Then I try to convert one of the columns to "factor". But as you can see below, I am not getting any "factor" type or classes. Am I doing anything wrong ? df[, 1] <- as.factor(df[, 1]) apply(df, 2, typeof) # aa bb #"character" "character" apply(df, 2, class) # aa bb #"character" "character" 回答1: Sorry I felt my

R: Why am I not getting type or class “factor” after converting columns to factor?

眉间皱痕 提交于 2020-01-23 13:37:07
问题 I have the following setup. df <- data.frame(aa = rnorm(1000), bb = rnorm(1000)) apply(df, 2, typeof) # aa bb #"double" "double" apply(df, 2, class) # aa bb #"numeric" "numeric" Then I try to convert one of the columns to "factor". But as you can see below, I am not getting any "factor" type or classes. Am I doing anything wrong ? df[, 1] <- as.factor(df[, 1]) apply(df, 2, typeof) # aa bb #"character" "character" apply(df, 2, class) # aa bb #"character" "character" 回答1: Sorry I felt my

R plyr, data.table, apply certain columns of data.frame

柔情痞子 提交于 2020-01-22 20:58:05
问题 I am looking for ways to speed up my code. I am looking into the apply / ply methods as well as data.table . Unfortunately, I am running into problems. Here is a small sample data: ids1 <- c(1, 1, 1, 1, 2, 2, 2, 2) ids2 <- c(1, 2, 3, 4, 1, 2, 3, 4) chars1 <- c("aa", " bb ", "__cc__", "dd ", "__ee", NA,NA, "n/a") chars2 <- c("vv", "_ ww_", " xx ", "yy__", " zz", NA, "n/a", "n/a") data <- data.frame(col1 = ids1, col2 = ids2, col3 = chars1, col4 = chars2, stringsAsFactors = FALSE) Here is a

Pandas: How to make apply on dataframe faster?

不打扰是莪最后的温柔 提交于 2020-01-22 07:29:05
问题 Consider this pandas example where I'm calculating column C by multiplying A with B and a float if a certain condition is fulfilled using apply with a lambda function: import pandas as pd df = pd.DataFrame({'A':[1,2,3,4,5,6,7,8,9],'B':[9,8,7,6,5,4,3,2,1]}) df['C'] = df.apply(lambda x: x.A if x.B > 5 else 0.1*x.A*x.B, axis=1) The expected result would be: A B C 0 1 9 1.0 1 2 8 2.0 2 3 7 3.0 3 4 6 4.0 4 5 5 2.5 5 6 4 2.4 6 7 3 2.1 7 8 2 1.6 8 9 1 0.9 The problem is that this code is slow and I

JavaScript 合并数组

三世轮回 提交于 2020-01-21 00:11:29
push() 方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。 < script > let arr1 = [ 1 , 2 , 3 ] let arr2 = [ 4 , 5 , 6 ] let total = arr1 . push ( arr2 ) console . log ( arr1 ) ; console . log ( arr2 ) ; console . log ( total ) ; < / script > 打印结果 apply() 方法 < script > let arr1 = [ 1 , 2 , 3 ] let arr2 = [ 4 , 5 , 6 ] Array . prototype . push . apply ( arr1 , arr2 ) // arr1.push.apply(arr1.arr2) // 简单写法 //arr1.push(4, 5, 6) // 等价于 console . log ( arr1 ) ; console . log ( arr2 ) ; < / script > /* 函数的 apply 方法有一个特性,那就是func.apply(obj,argv), argv是一个数组,调用 arr1.push这个函数实例的apply方法, 同时把 arr2 当作参数传入,这样 arr1.push 这个方法就会 遍历

Unexpected apply function behaviour in R

南笙酒味 提交于 2020-01-20 08:08:46
问题 I've discovered a surprising behaviour by apply that I wonder if anyone can explain. Lets take a simple matrix: > (m = matrix(1:8,ncol=4)) [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 We can flip it vertically thus: > apply(m, MARGIN=2, rev) [,1] [,2] [,3] [,4] [1,] 2 4 6 8 [2,] 1 3 5 7 This applies the rev() vector reversal function iteratively to each column. But when we try to apply rev by row we get: > apply(m, MARGIN=1, rev) [,1] [,2] [1,] 7 8 [2,] 5 6 [3,] 3 4 [4,] 1 2 .. a 90 degree

Unexpected apply function behaviour in R

不羁岁月 提交于 2020-01-20 08:08:46
问题 I've discovered a surprising behaviour by apply that I wonder if anyone can explain. Lets take a simple matrix: > (m = matrix(1:8,ncol=4)) [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 We can flip it vertically thus: > apply(m, MARGIN=2, rev) [,1] [,2] [,3] [,4] [1,] 2 4 6 8 [2,] 1 3 5 7 This applies the rev() vector reversal function iteratively to each column. But when we try to apply rev by row we get: > apply(m, MARGIN=1, rev) [,1] [,2] [1,] 7 8 [2,] 5 6 [3,] 3 4 [4,] 1 2 .. a 90 degree

django No migrations to apply 问题解决

匆匆过客 提交于 2020-01-20 07:09:26
最近在用django写项目,有的时候字段不够用,需要models增加字段,但是想回滚或者修改或者修改了属性等,例如忘了添加meta table于是操作了migrations 导致makemigrations没问题,migrate提示No migrations to apply 1.首先确认makemigrations的py是否存在,或者是否应当删除 这个时候再makemigrations一切顺利,但是执行migrate就除了问题 2.原因在数据库表django_migrations中存在,删除models的记录就可以了。 python manage.py makemigrations python manage.py migrate 来源: https://www.cnblogs.com/sevck/p/9719306.html