Can you please help me with the following issue. Imagine, I have a following df:
data = {
'A':['A1, B2, C', 'A2, A9, C', 'A3', 'A4, Z', 'A5, A1, Z'],
'B':['B1', 'B2', 'B3', 'B4', 'B4'],
}
df = pd.DataFrame(data)
How can I create a list with unique value that are stored in column 'A'? I want to smth like this:
list_A = [A1, B2, C, A2, A9, A3, A4, Z, A5]
Assuming you define as "values" the comma separated substrings, you can split, explode, and use unique:
list_A = df['A'].str.split(',\s*').explode().unique().tolist()
Output: ['A1', 'B2', 'C', 'A2', 'A9', 'A3', 'A4', 'Z', 'A5']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With