import pandas as pd
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],'year': [2000, 2001, 2002, 2001, 2002],'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
# 建立DataFrame对象
frame1 = pd.DataFrame(data)
In [73]: frame1
Out[73]:
pop state year
0 1.5 Ohio 2000
1 1.7 Ohio 2001
2 3.6 Ohio 2002
3 2.4 Nevada 2001
4 2.9 Nevada 2002
在建立过程中可以指点列的顺序:
frame1 = pd.DataFrame(data,columns=['year', 'state', 'pop'])
frame1
In [75]: frame1
Out[75]:
year state pop
0 2000 Ohio 1.5
1 2001 Ohio 1.7
2 2002 Ohio 3.6
3 2001 Nevada 2.4
4 2002 Nevada 2.9
和Series一样,DataFrame也是可以指定索引内容:
In [76]: ind = ['one', 'two', 'three', 'four', 'five']
In [77]: frame1 = DataFrame(data,index = ind)
In [78]: frame1
Out[78]:
pop state year
one 1.5 Ohio 2000
two 1.7 Ohio 2001
three 3.6 Ohio 2002
four 2.4 Nevada 2001
five 2.9 Nevada 2002