Reputation: 25
There are three columns:
A | B | Latest Date | Type |
---|---|---|---|
2021-05-28 | 2021-05-01 | 2021-05-28 | A |
2021-03-01 | 2021-03-30 | 2021-03-30 | B |
If "Latest Date" is same with A, the value of "Type" is A. If "Latest Date" is same with B, the value of "Type" is B.
How can I compare each columns A, B whether each element is same with Latest Date or not?
Upvotes: 2
Views: 99
Reputation: 9207
You can use the following, which can handle additional columns e.g. column C, D, E ... as well. And is able to give you both values if A and B would be identical.
df.join(df.reset_index().melt(id_vars=['index', 'Latest Date'], value_vars=['A','B'], var_name='Type').query('value==`Latest Date`').groupby(['index'])['Type'].apply(list))
A B Latest Date Type
0 2021-05-28 2021-05-01 2021-05-28 [A]
1 2021-03-01 2021-03-30 2021-03-30 [B]
Upvotes: 0
Reputation: 1598
You can use something like this:
df['type']= df.apply(lambda x: 'A' if x['A']==x['Latest_date'] else 'B')
Upvotes: 1
Reputation: 13831
You can use numpy.where
and compare Latest Date with A and B, and assign values in the Type column:
import numpy as np
df['Type'] = np.where(
df['Latest Date'].eq(df['A']),'A', # Return A if column Latest Date equals column A
np.where(
df['Latest Date'].eq(df['B']),'B', # Return B if column Latest Date equals column B
'Not A or B')) # If none are satisfied return 'Not A or B'
A B Latest Date Type
0 28/05/2021 01/05/2021 28/05/2021 A
1 01/03/2021 30/03/2021 30/03/2021 B
Upvotes: 0
Reputation: 23217
You can use .loc
to locate the rows with the respective conditions of column A
or column B
matching column Latest Date
and assign values to Type
accordingly, as follows:
df.loc[df['A'] == df['Latest Date'], 'Type'] = 'A'
df.loc[df['B'] == df['Latest Date'], 'Type'] = 'B'
Result:
print(df)
A B Latest Date Type
0 2021-05-28 2021-05-01 2021-05-28 A
1 2021-03-01 2021-03-30 2021-03-30 B
Upvotes: 1
Reputation: 11
data_list = [
{"A": "2021-05-28", "B": "2021-05-01", "latest_date": "2021-05-28", "type": ""},
{"A": "2021-03-01", "B": "2021-03-30", "latest_date": "2021-03-30", "type": ""}
]
for index, item in enumerate(data_list):
if item['latest_date'] == item['A']:
item['type'] = "A"
elif item['latest_date'] == item['B']:
item['type'] = "B"
data_list[index].update(item)
print(data_list)
Upvotes: 0