2

基于本教程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> 为什么列名顺序names1880.head(10)不正确?

代码打印:

Out[34]:

      births name   sex

我预计:

Out[34]:

      name sex births
4

1 回答 1

10

在这一行:

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

{'a','b','c'}语法使 aset和集合是无序的。

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

使用 alist或 atuple代替:

>>> ['name', 'sex', 'births']
['name', 'sex', 'births']
>>> ('name', 'sex', 'births')
('name', 'sex', 'births')
于 2013-03-08T22:05:11.513 回答