To_CSV unique values of a pandas column

IIUC, starting from a dataframe:

df = pd.DataFrame({'a':[1,2,3,4,5,6],'b':['a','a','b','c','c','b']})

you can get the unique values of a column with:

g = df['b'].unique()

that returns an array:

array(['a', 'b', 'c'], dtype=object)

to save it into a .csv file I would transform it into a Series s:

In [22]: s = pd.Series(g)

In [23]: s
Out[23]: 
0    a
1    b
2    c
dtype: object

So you can easily save it:

In [24]: s.to_csv('file.csv')

Hope that helps.


The pandas equivalent of np.unique is the drop_duplicates method.

In [42]: x = pd.Series([1,2,1,3,2])

In [43]: y = x.drop_duplicates()

In [46]: y
Out[46]: 
0    1
1    2
3    3
dtype: int64

Notice that drop_duplicates returns a Series, so you can call its to_csv method:

import pandas as pd
data = pd.read_csv('C:/Users/Z/OneDrive/Python/Exploratory Data/Aramark/ARMK.csv')
x = data.iloc[:,2]
y = x.drop_duplicates()
y.to_csv('yah.csv')