apply

Why there is an extra index when using apply in Pandas

佐手、 提交于 2020-01-04 04:54:11
问题 When I use apply to a user defined function in Pandas, it looks like python is creating an additional array. How could I get rid of it? Here is my code: def fnc(group): x = group.C.values out = x[np.where(x < 0)] return pd.DataFrame(out) data = pd.DataFrame({'A':np.random.randint(1, 3, 10), 'B':3, 'C':np.random.normal(0, 1, 10)}) data.groupby(by=['A', 'B']).apply(fnc).reset_index() There is this weird Level_2 index created. Is there a way to avoid creating it when running my function? A B

inplace apply to columns of pandas dataframe satisfying conditions

橙三吉。 提交于 2020-01-03 17:20:31
问题 Consider the following pandas dataframe: df = pd.DataFrame({'t': [1,2,3], 'x1': [4,5,6], 'x2': [7,8,9]} ) >>> print(df) t x1 x2 0 1 4 7 1 2 5 8 2 3 6 9 I would like to apply a function (say multiplying by 2) to those columns with names containing the character 'x' This can be done by: df.filter(regex='x').apply(lambda c: 2*c) but not in place. My solution is: tmp = df.filter(regex='x') tmp = tmp.apply(lambda c: 2*c) tmp['t'] = df['t'] df = tmp which has the added problem of changing the order

How to apply WPF ScrollBar style to a particular listview?

[亡魂溺海] 提交于 2020-01-03 16:53:15
问题 Ok, here is my scrollbar style. <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"> <Setter Property="Background" Value="#D5E0FF" /> </Style> If I apply this, all scrollbars in my application will get affected, undoubtedly. Now I have 2 listview(s) in my application, I need to apply this style to only a particular listview, while another one remain default scrollbar, any idea? This is driving me crazy. Thanks. 回答1: Add the style as a resource to the ListView itself. <ListView>

How to apply WPF ScrollBar style to a particular listview?

一笑奈何 提交于 2020-01-03 16:50:54
问题 Ok, here is my scrollbar style. <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"> <Setter Property="Background" Value="#D5E0FF" /> </Style> If I apply this, all scrollbars in my application will get affected, undoubtedly. Now I have 2 listview(s) in my application, I need to apply this style to only a particular listview, while another one remain default scrollbar, any idea? This is driving me crazy. Thanks. 回答1: Add the style as a resource to the ListView itself. <ListView>

Use function instead of for loop in R: substracting previous rows iteratively with exceptions

两盒软妹~` 提交于 2020-01-03 03:27:30
问题 I have a large dataset for which I want to get the value of each row minus the following row except for each fifth row. With a for loop, it is fairly simple but with my large dataset, it takes over an hour. I've been told that apply with a function is MUCH faster, but I don't know how to write a complicated function and I can't find examples of similar problems. #set up matrix x=matrix(0,15,2) x[,1]=c(1, 5, 4, 3, 4, 2, 4, 3, 7, 8, 3, 2, 9, 7, 3) #run for loop for (i in c(0:((nrow(x)/5)-1)*5))

Apply lines() to columns of a data frame/matrix; each line with a different color

前提是你 提交于 2020-01-02 06:15:47
问题 I am trying to come up with a solution that doesn't involve using other packages such as ggplot. While plotting multiple lines is pretty straightforward, I haven't figured out a way to apply different values of an argument - e.g., different colors - to different lines. The code below (with the resulting plot) was my attempt, which obviously didn't do what I would like it to do. I also don't want to use a loop because I am trying to make my script as simple as possible. df = cbind(sort(rnorm

Haskell - apply tuple of functions to tuple of values?

房东的猫 提交于 2020-01-02 05:19:08
问题 I have a tuple of values representing some state, and want to translate it by an addition (shift). My values are a longer version of ( Int, [Int], Int), and I want something conceptually (but not literally) like this: shift n = ??? (+n) (id, map, id) -- simple(?) which would be equivalent to: shift n (a, b, c) = (a+n, map (+n) b, c+n) I am happy to just go with this explicit function usage, but wondered it there was a more idiomatic point-free version using Applicative or Arrows or ..., or if

using apply with assign in R

喜你入骨 提交于 2020-01-02 03:34:08
问题 Consider the following example: Vars <- c("car","bike","lorry") Dat <- c(10,20,22) for (i in 1:length(Vars)){ assign(Vars[i],Dat[i]) } Here, I would like to generate three variables in the workspace named according to the entries in Vars and the values in Dat . At the moment I am using a loop, but I have been trying to remove the loop by using apply, how would be the best way of doing this? 回答1: This is a great example of when to use a for loop instead of an apply . The best solution is to

Pandas groupby/apply has different behaviour with int and string types

对着背影说爱祢 提交于 2020-01-01 17:09:15
问题 I have the following dataframe X Y 0 A 10 1 A 9 2 A 8 3 A 5 4 B 100 5 B 90 6 B 80 7 B 50 and two different functions that are very similar def func1(x): if x.iloc[0]['X'] == 'A': x['D'] = 1 else: x['D'] = 0 return x[['X', 'D']] def func2(x): if x.iloc[0]['X'] == 'A': x['D'] = 'u' else: x['D'] = 'v' return x[['X', 'D']] Now I can groupby/apply these functions df.groupby('X').apply(func1) df.groupby('X').apply(func2) The first line gives me what I want, i.e. X D 0 A 1 1 A 1 2 A 1 3 A 1 4 B 0 5

Help me replace a for loop with an “apply” function

和自甴很熟 提交于 2020-01-01 12:14:33
问题 ...if that is possible My task is to find the longest streak of continuous days a user participated in a game. Instead of writing an sql function, I chose to use the R's rle function, to get the longest streaks and then update my db table with the results. The (attached) dataframe is something like this: day user_id 2008/11/01 2001 2008/11/01 2002 2008/11/01 2003 2008/11/01 2004 2008/11/01 2005 2008/11/02 2001 2008/11/02 2005 2008/11/03 2001 2008/11/03 2003 2008/11/03 2004 2008/11/03 2005