I am currently trying to populate a dropdown list with labels that need to exist in a second dataframe, currently, the below is returning a True and False option:
[{'label': i, 'value': i} for i in sorted(dff.Opp.isin(df2['Name']).unique().astype(str))],
any advice on what needs to be changed?
EDIT: DataFrame examples:
df1:
0 Opp
1 Test2
2 Test4
3 Test6
4 Test7
df2:
0 Name
1 Test1
2 Test2
3 Test3
4 Test4
5 Test5
Expected outcome: dropdown would be populated with Test2 and Test4 as they are in both dataframes
CodePudding user response:
I assume that you're currently working with pandas. I tried to reproduce your issue with the code below. From the official document,
pandas.Series.isin
returns Series of booleans indicating if each element is in values.
you need to filter values in the Series with loc
function before calling .unique()
dff = pd.DataFrame(data={"Opp": ["a", "b", "c", "d", "e"]})
df2 = pd.DataFrame(data={"Name": ["a", "e"]})
dff.loc[dff['Opp'].isin(df2['Name']), 'Opp'].unique()
# Output:
# array(['a', 'e'], dtype=object)
Solution
[{'label': i, 'value': i} for i in sorted(dff.loc[dff['Opp'].isin(df2['Name']), 'Opp'].unique())]
# Output:
# [{'label': 'a', 'value': 'a'}, {'label': 'e', 'value': 'e'}]