2

Based on this tutorial http://www.youtube.com/watch?v=aKs1Vgtb1do:

import pandas as pd
names1880 = pd.read_csv('yob1880.txt', names={'name', 'sex', 'births'})
names1880.head(10)

Out[34]:

      births name   sex
0       Mary    F  7065
1       Anna    F  2604
2       Emma    F  2003
3  Elizabeth    F  1939
4     Minnie    F  1746
5   Margaret    F  1578
6        Ida    F  1472
7      Alice    F  1414
8     Bertha    F  1320
9      Sarah    F  1288

ubuntu@ubuntu-VirtualBox:~/P4DA$ head yob1880.txt 
Mary,F,7065
Anna,F,2604
Emma,F,2003
Elizabeth,F,1939
Minnie,F,1746
Margaret,F,1578
Ida,F,1472
Alice,F,1414
Bertha,F,1320
Sarah,F,1288

Question> Why the column name order of names1880.head(10) is not correct?

The code prints:

Out[34]:

      births name   sex

I expect:

Out[34]:

      name sex births

1 Answer 1

10

In this line:

names1880 = pd.read_csv('yob1880.txt', names={'name', 'sex', 'births'})

The {'a','b','c'} syntax makes a set, and sets are unordered.

>>> {'name', 'sex', 'births'}
set(['births', 'name', 'sex'])

Use a list or a tuple instead:

>>> ['name', 'sex', 'births']
['name', 'sex', 'births']
>>> ('name', 'sex', 'births')
('name', 'sex', 'births')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.