Reputation: 3215
I have two columns containing tuples within a single DataFrame
object.
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)
I need to return the tuple containing the higher number to a new column. It does not matter if the old columns remain within df
or not
df max_value
('chicken wing', 1)
('mason', 0.97)
('lost in space', 0.47)
('marvelous', 1)
Upvotes: 1
Views: 2205
Reputation: 394099
You could do it like this:
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
So here we use a lambda to compare the tuples for each row to generate a boolean mask, and then use this with where
to return column a if True
otherwise return column 'b'
The output from the apply
:
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
A more performant approach would be to extract the percentages into separate columns so you can use a vectorised approach in the comparison:
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)
You can also define a custom func to to handle a dynamic number of cols and use 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
Upvotes: 1
Reputation: 963
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)
Upvotes: 1
Reputation: 3959
try this
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)
Upvotes: 1