Python-比较数据帧元组值

Python-比较数据帧元组值,python,pandas,max,Python,Pandas,Max,在一个DataFrame对象中有两列包含元组 df a b ('chicken wing', 1) ('saucy', 0.35) ('burger', 0.85) ('mason', 0.97) ('burping', 0.37) ('lost in space', 0.47) ('marvelous', 1) ('tr

在一个
DataFrame
对象中有两列包含元组

 df    a                         b
      ('chicken wing', 1)        ('saucy', 0.35)
      ('burger', 0.85)           ('mason', 0.97)
      ('burping', 0.37)          ('lost in space', 0.47)
      ('marvelous', 1)           ('tremendous', .85)
我需要将包含较高数字的元组返回到新列。旧列是否保留在
df
中并不重要

结果
你可以这样做:

In [1]: df['a'].where( df.apply(lambda row: row['a'][1] > row['b'][1], axis=1), df['b'])

Out [1]: 

0        (chicken wing, 1)
1            (mason, 0.97)
2    (lost in space, 0.47)
3           (marvelous, 1)
Name: a, dtype: object
因此,这里我们使用lambda来比较每一行的元组以生成一个布尔掩码,然后使用它返回列a if
True
否则返回列“b”

应用程序的输出:

In[3]:
df.apply(lambda row: row['a'][1] > row['b'][1], axis=1)

Out[3]: 
0     True
1    False
2    False
3     True
dtype: bool
更有效的方法是将百分比提取到单独的列中,以便在比较中使用矢量化方法:

In[4]:
df['a_%'] = df['a'].apply(lambda x: x[1])
df['b_%'] = df['b'].apply(lambda x: x[1])
df

Out[4]: 
                   a                      b   a_%   b_%
0  (chicken wing, 1)          (saucy, 0.35)  1.00  0.35
1     (burger, 0.85)          (mason, 0.97)  0.85  0.97
2    (burping, 0.37)  (lost in space, 0.47)  0.37  0.47
3     (marvelous, 1)     (tremendous, 0.85)  1.00  0.85

In[5]:
df['max_value'] = df['a'].where(df['a_%'] > df['b_%'], df['b'])
df

Out[5]: 
                   a                      b   a_%   b_%              max_value
0  (chicken wing, 1)          (saucy, 0.35)  1.00  0.35      (chicken wing, 1)
1     (burger, 0.85)          (mason, 0.97)  0.85  0.97          (mason, 0.97)
2    (burping, 0.37)  (lost in space, 0.47)  0.37  0.47  (lost in space, 0.47)
3     (marvelous, 1)     (tremendous, 0.85)  1.00  0.85         (marvelous, 1)
您还可以定义自定义func来处理动态数量的列,并使用
max

In[11]:
def func(x):
    vals = [y[1] for y in x]
    return x[vals.index(max(vals))]
df.apply(lambda row: func(row), axis=1)

Out[11]: 
0        (chicken wing, 1)
1            (mason, 0.97)
2    (lost in space, 0.47)
3           (marvelous, 1)
dtype: object
试试这个

def compare_tuples(row):
    if row['a'][1] >= row['b'][1]:
        return row['a']
    else:
        return row['b']
df['larger'] = df.apply(compare_tuples, axis=1)

聪明!我需要学习如何思考numpy风格,因为我觉得性能会比普通的
apply
更好。您需要先将元组中的百分比提取到单独的列中,将非标量值存储在pandas数据帧中是无性能的请参阅更新的答案以获得更高性能的方法,尽管这将涉及添加额外的列。有可能在两列之间使用
max
,而不是直接与
运算符进行比较?您必须定义自定义func并调用使用
apply
,我将添加一个更新
def compare_tuples(row):
    if row['a'][1] >= row['b'][1]:
        return row['a']
    else:
        return row['b']
df['larger'] = df.apply(compare_tuples, axis=1)
In [1]: import pandas as pd

In [2]: df = pd.DataFrame({"a" : [('chicken wing', 1), ('burger', 0.85), ('burping', 0.37), ('marvelous', 1)], "b": [('saucy', 0.35), ('mason', 0.97), ('lost in space', 0.47), ('tremendous', .85)]})

In [3]: df['max_value'] = [a_value if (a_value[1] > b_value[1]) else b_value for a_value, b_value in zip(df.a, df.b)]

In [4]: df
Out[4]: 
                   a                      b              max_value
0  (chicken wing, 1)          (saucy, 0.35)      (chicken wing, 1)
1     (burger, 0.85)          (mason, 0.97)          (mason, 0.97)
2    (burping, 0.37)  (lost in space, 0.47)  (lost in space, 0.47)
3     (marvelous, 1)     (tremendous, 0.85)         (marvelous, 1)