简体   繁体   English

如何对照另一个列检查pandas数据框列的值并在第三列中操作该值

[英]How can I check the value of a pandas dataframe column against another column and manipulate the value in a third column

I have a pandas dataframe 我有一个熊猫数据框

dfcolour: dfcolour:

     A      B      C       D     E
0   red     0     redy     1    red
1   blue    1     bluey    2    bluey
2   green   0     greeny   0    greenz

I want to check the values in column E and compare them to columns A & C. If the value in E matches the value in A for the same row then I want to increment the value in B, if it matches the value in C for the same row, then I will increment the value in D, if it doesn't match either, I want to create 2 new columns F & G with F having the new string and G as an integer column being incremented starting at 0 我想检查E列中的值,并将它们与A和C列进行比较。如果E中的值与同一行的A中的值匹配,那么我想增加B中的值,如果它与C中的值匹配同一行,然后我将增加D中的值,如果两者都不匹配,我想创建2个新列F&G,其中F具有新字符串,而G作为整数列从0开始递增

The new dfcolour will look like: 新的dfcolour如下所示:

     A      B      C       D     E       F        G
0   red     1     redy     1    red               0
1   blue    1     bluey    3    bluey             0
2   green   0     greeny   0    greenz   greenz   1

Is it possible to do this without iterating? 是否可以不重复进行此操作?

Code to create dataframe: 创建数据框的代码:

dfObject = pd.DataFrame()
dfObject.set_value(1, 'A', 'red')
dfObject.set_value(1, 'B', 0)
dfObject.set_value(1, 'C', 'redy')
dfObject.set_value(1, 'D', 1)
dfObject.set_value(1, 'E', 'red')
dfObject.set_value(2, 'A', 'blue')
dfObject.set_value(2, 'B', 1)
dfObject.set_value(2, 'C', 'bluey')
dfObject.set_value(1, 'D', 2)
dfObject.set_value(1, 'E', 'bluey')
dfObject.set_value(3, 'A', 'green')
dfObject.set_value(3, 'B', 0)
dfObject.set_value(3, 'C', 'greeny')
dfObject.set_value(1, 'D', 0)
dfObject.set_value(1, 'E', 'greenz')

You can create those conditions and use numpy.where to construct new columns: 您可以创建这些条件并使用numpy.where构造新列:

AE = df.A == df.E
CE = df.C == df.E
df['B'] += AE          # if A == E, add one to B
df['D'] += CE          # if C == E, add one to D
df['F'] = pd.np.where(~(AE|CE), df.E, '')   # else create F
df['G'] = pd.np.where(~(AE|CE), 1, 0)       # else create G
​    
df
#      A    B        C  D        E       F  G
#0   red    1     redy  1      red          0
#1  blue    1    bluey  3    bluey          0
#2  green   0   greeny  0   greenz  greenz  1

Well sorry, my first idea was to do a kind of iteration: you can apply a function to the rows of the DataFrame , and return one or more columns. 很抱歉,我的第一个想法是进行某种迭代:您可以一个函数应用于DataFrame的行,并返回一个或多个列。 This is usually how I do it. 通常这就是我的方法。 It is still iterating, but kind of a "better" way to do than using iterrows . 它仍然在迭代,但是比使用iterrows “更好”。

def special_function(row):
    b = row['B']
    d = row['D']
    f = None
    g = 0
    if row['E'] == row['A']:
        b = b + 1
    elif row['E'] == row['C']:
        d = d + 1
    else:
        f = row['E']
        g = 1
    return pandas.Series({ 'B':b, 'D':d, 'F': f, 'G': g })

dfcolour[['B', 'D', 'F', 'G']] = dfcolour.apply(special_function, axis=1)

暂无
暂无

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

相关问题 我如何对照自身检查Pandas DataFrame的列? - How can I check a Pandas DataFrame's column against itself? Pandas - 检查另一个数据框列中的系列值 - Pandas - check for series value in another dataframe column 如何对与pandas DataFrame中另一列的特定值对应的列值求和? - How can I sum column values that corrispond to a specific value of another column in a pandas DataFrame? 如何遍历pandas DataFrame的一列并从另一列返回值? - How can I iterate through a column of a pandas DataFrame and return value from another column? 检查值是否在 Pandas dataframe 列中 - Check if value is in Pandas dataframe column Pandas Dataframe 检查列值是否在列列表中 - Pandas Dataframe Check if column value is in column list Pandas 数据框:根据另一列中的值操作列(不迭代行) - Pandas dataframe: Manipulate column depending on value in another column (without iterating over rows) 将 DataFrame 列值与另一个 DataFrame 列匹配并计数命中 - Match DataFrame column value against another DataFrame column and count hits 如何在另一个数据帧列pandas中检查一个数据帧的列值多少次? - how to check column value of one data frame how many times in another dataframe column pandas? 如何在熊猫数据框中找到与另一列中的多个值相对应的列中具有值的所有行? - How can I find all rows with a value in one column which corresponds to more than one value in another column in a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM