Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of unique values within a pandas column

Tags:

python

pandas

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]
like image 952
Alberto Alvarez Avatar asked Oct 25 '25 19:10

Alberto Alvarez


1 Answers

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']

like image 143
mozway Avatar answered Oct 28 '25 09:10

mozway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!