apply

python之padnas学习(四)

匿名 (未验证) 提交于 2019-12-02 22:11:45
A.整数索引: 用整数索引在pandas中 与python的list与truple有一点不同,如果在Series的默认row Index为数字的话,用整数索引会出错,那么我们就应该将index更改掉,改成其它。 1. import numpy as np import pandas as pd obj=pd.Series(np.arange(3.)) print(obj) print(obj[-1]) 看得出来如果这样会报错! 因为在我们创建Series的时候index 为0,1,2 .而我们obj[-1] 表达的意思和它起冲突了,所以程序想知道我们想要什么就困难。我们需要的倒数第一个,而它以为我们是要index为-1的这个数据,所以就会报错。那么我们创建时将index更改掉,那么就可以了! obj1=pd.Series(np.arange(3.),index=['a','b','c']) print(obj1[-1]) 2. 其实也可以这样索引,从输出中看出每个的不同,这里我用的是创建的第一个对象obj,不是obj1 print(obj[:1]) print(obj.loc[:1]) print(obj.iloc[:1]) B.算数与数据对齐: padnas一个特点是,两个相同的数据结构相加,那么他们的索引值对应相加,如果这两个,其中一个有不同的index,那么相加后

Apply over two data frames

旧街凉风 提交于 2019-12-02 21:15:06
I'm using R, and I have two data.frames, A and B . They both have 6 rows, but A has 25000 columns (genes), and B has 30 columns. I'd like to apply a function with two arguments f(x,y) where x is every column of A and y is every column of B . So far it looks like this: i = 1 for (x in A){ j = 1 for (y in B){ out[i,j] <- f(x,y) j = j + 1 } i = i + 1 } I have two issues with this: from my Python programming I associate keeping track of counters like this as crufty, and from my R programming I am nervous of for loops. However, I can't quite see how to apply apply (or even if I should apply apply )

Refused to apply style from &lt;URL&gt; because its MIME type (&#039;text/html&#039;) is not a supported stylesheet

匿名 (未验证) 提交于 2019-12-02 20:32:16
问题描述:css和js文件导入不到html中,出现错误:10Refused to apply style from ‘’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled. 在引入css和js库之前添加代码 解决问题 参考资料 文章来源: https://blog.csdn.net/wongnoubo/article/details/89716000

R - apply lm on each data frame row

对着背影说爱祢 提交于 2019-12-02 20:15:52
问题 I am trying to apply a simple linear regression between two columns of a data frame, for every row. After some research I feel like I am almost there, but my function still doesn't work. Please take a look: set.seed(1) DF <- data.frame(A=rnorm(50, 100, 3), B=rnorm(50, 100, 3)) resultlist <- apply(DF, 1, function(y) lm(y ~ x)) resultcoeffs <- apply(DF, 1, function(y) lm(y ~ x)$coefficients) Any tip on how to achieve that? Thanks in advance. 回答1: It is just one observation per row. Note that

Selecting rows from a data frame from combinations of lists [duplicate]

雨燕双飞 提交于 2019-12-02 19:32:32
问题 This question already has answers here : Removing one table from another in R [closed] (3 answers) Closed 2 years ago . I have a dataframe, dat: dat<-data.frame(col1=rep(1:4,3), col2=rep(letters[24:26],4), col3=letters[1:12]) I want to filter dat on two different columns using ONLY the combinations given by the rows in the data frame filter : filter<-data.frame(col1=1:3,col2=NA) lists<-list(list("x","y"),list("y","z"),list("x","z")) filter$col2<-lists So for example, rows containing (1,x) and

Apply a function to all the elements of a data frame

不羁岁月 提交于 2019-12-02 19:27:40
I am trying to apply some transformations to all the elements in a dataframe. When using the regular apply functions, I get a matrix back and not a dataframe. Is there a way to get a dataframe directly without adding as.data.frame to each line? df = data.frame(a = LETTERS[1:5], b = LETTERS[6:10]) apply(df, 1, tolower) #Matrix apply(df, 2, tolower) #Matrix sapply(df, tolower) #Matrix as.data.frame(sapply(df, tolower)) # Can I avoid "as.data.frame"? We can use lapply and assign it back to 'df' df[] <- lapply(df, tolower) The [] preserves the same structure as the original dataset. Using apply

select data from mysql (with for loop(?)) in R

血红的双手。 提交于 2019-12-02 18:46:04
问题 What I want is: m1 <- dbGetQuery(mydb, "select out,in from table where value > 1") m2 <- dbGetQuery(mydb, "select out,in from table where value > 1.1") m3 <- dbGetQuery(mydb, "select out,in from table where value > 1.2") m4 <- dbGetQuery(mydb, "select out,in from table where value > 1.3") . . . m101 <- dbGetQuery(mydb, "select out,in from table where value > 10") then n1 <- degree(graph.data.frame(m)) n2 <- degree(graph.data.frame(m2) . . . I would like to simplify these codes with apply

Row-wise iteration like apply with purrr

风流意气都作罢 提交于 2019-12-02 17:39:47
How do I achieve row-wise iteration using purrr::map? Here's how I'd do it with a standard row-wise apply. df <- data.frame(a = 1:10, b = 11:20, c = 21:30) lst_result <- apply(df, 1, function(x){ var1 <- (x[['a']] + x[['b']]) var2 <- x[['c']]/2 return(data.frame(var1 = var1, var2 = var2)) }) However, this is not too elegant, and I would rather do it with purrr. May (or may not) be faster, too. You can use pmap for row-wise iteration. The columns are used as the arguments of whatever function you are using. In your example you would have a three-argument function. For example, here is pmap

Two data frames, join ID's and position from one df if value is in the range of 2nd df

烂漫一生 提交于 2019-12-02 16:13:26
问题 Since yesterday I've been trying to solve my problem... I think, the best explanation will be when I show you the example, so I have two df's x and y and they look like this: ID_x <- c('x1','x2','x3','x4','x5','x6','x7','x8','x9','x10') chr_x <- c('chr1','chr1','chr1','chr1','chr1','chr1','chr1','chr1','chrX','chrY') start <- c(1,2,3,4,50,60,70,80,9,100) stop <- c(10,20,30,40,500,600,700,800,95,1000) x <- data.frame(ID_x,chr_x,start,stop) > x ID_x chr_x start stop 1 x1 chr1 1 10 2 x2 chr1 2

Append onclick method using for loop

我是研究僧i 提交于 2019-12-02 15:53:06
问题 I'm appending onclick events to elements that I'm creating dynamically. I'm using the code below, this is the important part only. Test.prototype.Show= function (contents) { for (i = 0; i <= contents.length - 1; i++) { var menulink = document.createElement('a'); menulink.href = "javascript:;"; menulink.onclick = function () { return that.ClickContent.apply(that, [contents[i]]); }; } } First it says that it's undefined. Then I changed and added: var content = content[i]; menulink.onclick =