KcH
KcH

Reputation: 3502

creating a new dataframe from the unique values of another dataframe column

Question may be a duplicate one but they are not as simple as this, I have a dataframe (df) as:

city | address |. . .
hyd     .
hyd     . 
sec     . 
los
los
miy

. . .

I have tried my luck as this:

df1['unique']=df.city.unique()

How may I achieve a new dataframe df1 having unique city names of df['city'] as

city
hyd
sec
miy
.
.

Upvotes: 1

Views: 4767

Answers (1)

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

try this,

df1 = pd.DataFrame(df.city.dropna().unique(), columns=['unique'])

O/P:

  unique
0    hyd
1    sec
2    los
3    miy

If you want it as Series,

s = pd.Series(df.city.dropna()unique(), name= 'series')

0    hyd
1    sec
2    los
3    miy
Name: series, dtype: object

Upvotes: 3

Related Questions