简体   繁体   English

如何根据相应系列列中的值对一系列列应用函数?

[英]How to apply a function on a series of columns, based on the values in a corresponding series of columns?

I have a df where I have several columns, that, based on the value (1-6) in these columns, I want to assign a value (0-1) to its corresponding column.我有一个 df,其中有几列,根据这些列中的值 (1-6),我想为其相应的列分配一个值 (0-1)。 I can do it on a column by column basis but would like to make it a single function.我可以逐列进行,但希望将其设为单一功能。 Below is some example code:下面是一些示例代码:

import pandas as pd
df = pd.DataFrame({'col1': [1,3,6,3,5,2], 'col2': [4,5,6,6,1,3], 'col3': [3,6,5,1,1,6],
                  'colA': [0,0,0,0,0,0], 'colB': [0,0,0,0,0,0], 'colC': [0,0,0,0,0,0]})

(col1 corresponds with colA, col2 with colB, col3 with colC) (col1 对应 colA,col2 对应 colB,col3 对应 colC)

This code works on a column by column basis:此代码逐列工作:

df.loc[(df.col1 != 1) & (df.col1 < 6), 'colA'] = (df['colA']+ 1)

But I would like to be able to have a list of columns, so to speak, and have it correspond with another.但我希望能够有一个列列表,可以这么说,并让它与另一个相对应。 Something like this, (but that actually works):像这样的东西,(但实际上有效):

m = df['col1' : 'col3'] != 1 & df['col1' : 'col3'] < 6
df.loc[m, 'colA' : 'colC'] += 1

Thank You!谢谢你!

Idea is filter both DataFrame s by DataFrame.loc , then filter columns by mask and rename columns by another df2 and last use DataFrame.add only for df.columns :想法是通过DataFrame.loc过滤两个DataFrame s,然后通过掩码过滤列并通过另一个df2重命名列,最后仅将DataFrame.add用于df.columns

df1 = df.loc[:, 'col1' : 'col3'] 
df2 = df.loc[:, 'colA' : 'colC']

d = dict(zip(df1.columns,df2.columns))

df1 = ((df1 != 1) & (df1 < 6)).rename(columns=d)

df[df2.columns] = df[df2.columns].add(df1)
print (df)
   col1  col2  col3  colA  colB  colC
0     1     4     3     0     1     1
1     3     5     6     1     1     0
2     6     6     5     0     0     1
3     3     6     1     1     0     0
4     5     1     1     1     0     0
5     2     3     6     1     1     0

Here's what I would do:这是我会做的:

# split up dataframe
sub_df = df.iloc[:,:3]
abc = df.iloc[:,3:]

# make numpy array truth table
truth_table = (sub_df.to_numpy() > 1) & (sub_df.to_numpy() < 6)

# redefine abc based on numpy truth table
new_abc = pd.DataFrame(truth_table.astype(int), columns=['colA', 'colB', 'colC'])

# join the updated dataframe subgroups
new_df = pd.concat([sub_df, new_abc], axis=1)

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

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