Here is the original data:
JavaScript
x
8
1
Name Wine Year
2
0 Mark Volnay 1983
3
1 Mark Volnay 1979
4
3 Mary Volnay 1979
5
4 Mary Volnay 1999
6
5 Mary Champagne 1993
7
6 Mary Champagne 1989
8
I would like to be able to get the value of Year
in function of the values of Name
and Wine
. It would return all the values in the Year
column of the entries that have the corresponding values in the Name
and Wine
columns.
For example: with the key ['Mark', 'Volnay']
I would get the values [1983, 1979]
I tried manipulating the data and here is the best I could get.
Keep one instance of each key:
JavaScript
1
5
1
Name Wine Year
2
1 Jean Volnay 1979
3
4 Pierre Volnay 1999
4
6 Pierre Champagne 1989
5
Remove the Year
column
JavaScript
1
5
1
Name Wine
2
1 Jean Volnay
3
4 Pierre Volnay
4
6 Pierre Champagne
5
Get the values in a list
JavaScript
1
2
1
[['Mark', 'Volnay'], ['Mary', 'Volnay'], ['Mary', 'Champagne']]
2
I now have the keys I need, but I can’t get the values in the original dataframe in function of the value of the key.
Advertisement
Answer
You can also use groupby
with get_group
JavaScript
1
5
1
def getyear(datafrae,keys:list):
2
values = df.groupby(['Name', 'Wine']).get_group(tuple(key))['Year']
3
dedupvalues = [*dict.fromkeys(values).keys()] #incase of duplicates
4
return dedupvalues
5
JavaScript
1
4
1
keys = ['Mark', 'Volnay']
2
print(getyear(df,keys))
3
[1983, 1979]
4