Python Forum

Full Version: How to show unique names in a table?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there, I have follow table

State Gender Year Top Name Occurences
0 AK F 1910 Mary 14
1 AK F 1911 Mary 12
2 AK F 1912 Mary 9
3 AK F 1913 Mary 21
4 AK F 1914 Mary 22
5 AK F 1915 Mary 23
6 AK F 1916 Mary 18
7 AK F 1917 Mary 21
8 AK F 1918 Mary 27

I want to see only one column with the names unique
this is my code:

[python[import pandas as pd
babynames = pd.read_csv('TopBabyNamesbyState.csv')
babynames = pd.unique(pd.Series(['Top Name']))
print(babynames)[/python]

With this code I get the number of all rows in the table.
What is the right code?

Thank you!
J
To get unique names of the specific column of a data frame, you need to call method .unique(),
e.g. babynames = pd.read_csv('TopBabyNamesbyState.csv')['Top Name'].unique(); If this doesn't work,
the file probably wasn't properly parsed by read_csv (check sep argument and format of your file).