from pandas import Series, DataFrame
col = [‘a’,’b’,’c’]
data = DataFrame([[1,2,3],[10,11,12],[20,21,22]],columns=col)
data
Out[34]:
a b c
0 1 2 3
1 10 11 12
2 20 21 22
自动加index
data.dtypes
Out[42]:
a int64
b int64
c int64
dtype: object
data[‘a’]
Out[43]:
0 1
1 10
2 20
Name: a, dtype: int64
data2 = data.set_index(‘a’)
In [46]: data2
Out[46]:
b c
a
1 2 3
10 11 12
20 21 22
把某一列变成index
data2.index.name = None
In [48]: data2
Out[48]:
b c
1 2 3
10 11 12
20 21 22
可以去掉index name
增加一列:
data[‘time’]=1
data
Out[51]:
a b c time
0 1 2 3 1
1 10 11 12 1
2 20 21 22 1
把某一列取值index
data[‘time’]=data.index
删除一列: