简体   繁体   English

Pandas:基于多个不同的列创建列

[英]Pandas: Creating column based on multiple different columns

Ok im feeling like this one is very easy and there should also be the right answer in a previous thread, but apparently I couldnt manage to find the answer by myself or in a thread.好的,我觉得这个很容易,并且在以前的线程中也应该有正确的答案,但显然我无法自己或在线程中找到答案。 Here is what I got: I've got a dataframe with different samples belonging to groups这是我得到的:我有一个 dataframe 不同的样本属于组

pd.DataFrame({'sample1': [1,2,3], 'sample2':[2,4,6], 'sample3':[4,4,4], 'sample4':[6,6,6], 'divisor':[1,2,1]})
groups=[["sample1","sample2"],["sample3","sample4"]]

I want the code te create a new column for each sample dependent on the sum of the group where this sample is in. Result should be 0 if quotient is below 0 or else should be original value.我希望代码根据该样本所在组的总和为每个样本创建一个新列。如果商低于 0,则结果应为 0,否则应为原始值。 This first part perfectly does the summing:第一部分完美地进行了求和:

for i in range(len(groups)):
    df["groupsum"+str(i)]=df[groups[i]].sum(axis=1)

    for sample in groups[i]:
        df[sample+"_corr"]=""
        df[sample+"_corr"]= df[sample].apply(lambda x: 0 if (df["groupsum"+str(i)]/df["divisor"])<4 else df[sample])

I get the error:我得到错误:

 ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

So whats the right way to handle this?那么处理这个问题的正确方法是什么? Thanks a lot in advance非常感谢提前

just use np.wehere instead of looping the dataframe with apply:只需使用np.wehere而不是使用应用循环 dataframe:

df[sample+"_corr"]= np.where((df["groupsum"+str(i)]/df["divisor"])<4 , 0 , df[sample])

Output: Output:

    sample1 sample2 sample3 sample4 divisor groupsum0   sample1_corr    sample2_corr    groupsum1   sample3_corr    sample4_corr
0   1   2   4   6   1   3   0   0   10  4   6
1   2   4   4   6   2   6   0   0   10  4   6
2   3   6   4   6   1   9   3   6   10  4   6

this is also better performance because apply is very slow solution and should be avoided when possible.这也是更好的性能,因为 apply 是非常慢的解决方案,应尽可能避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM