import pandas as pd
from pandas import Series, DataFrame
import numpy as np
data = DataFrame(np.arange().reshape(,),index=['one','two','three'],columns=['a','b','c','d','e'])
#index或者columns預設條件下,可以用實驗看看
print(data)
輸出為:
a b c d e
one
two
three
附轉載如下:(可閱讀學習)
#對列的操作方法有如下幾種
data.icol() #選取第一列
E:\Anaconda2\lib\site-packages\spyder\utils\ipython\start_kernel.py:: FutureWarning: icol(i) is deprecated. Please use .iloc[:,i]
# -*- coding: utf-8 -*-
Out[]:
one
two
three
Name: a, dtype: int32
data['a']
Out[]:
one
two
three
Name: a, dtype: int32
data.a
Out[]:
one
two
three
Name: a, dtype: int32
data[['a']]
Out[]:
a
one
two
three
data.ix[:,[,,]] #不知道列名隻知道列的位置時
Out[]:
a b c
one
two
three
data.ix[,[]] #選擇第2行第1列的值
Out[]:
a
Name: two, dtype: int32
data.ix[[,],[]] #選擇第2,3行第1列的值
Out[]:
a
two
three
data.ix[:,[,]] #選擇第2-4行第1、3列的值
Out[]:
a c
two
three
data.ix[:,:] #選擇第2-3行,3-5(不包括5)列的值
Out[]:
c d
two
data.ix[data.a>,]
Out[]:
three
Name: d, dtype: int32
data.ix[data.b>,:] #選擇'b'列中大于6所在的行中的第4列,有點拗口
Out[]:
d
three
data.ix[data.a>,:] #選擇'a'列中大于5所在的行中的第3-5(不包括5)列
Out[]:
c d
three
data.ix[data.a>,[,,]] #選擇'a'列中大于5所在的行中的第2列并重複3次
Out[]:
c c c
three
#還可以行數或列數跟行名列名混着用
data.ix[:,['a','e']]
Out[]:
a e
two
three
data.ix['one':'two',[,]]
Out[]:
c b
one
two
data.ix[['one','three'],[,]]
Out[]:
c c
one
three
data.ix['one':'three',['a','c']]
Out[]:
a c
one
two
three
data.ix[['one','one'],['a','e','d','d','d']]
Out[]:
a e d d d
one
one
#對行的操作有如下幾種:
data[:] #(不知道列索引時)選擇第2行,不能用data[1],可以用data.ix[1]
Out[]:
a b c d e
two
data.irow() #選取第二行
Out[]:
a
b
c
d
e
Name: two, dtype: int32
data.ix[] #選擇第2行
Out[]:
a
b
c
d
e
Name: two, dtype: int32
data['one':'two'] #當用已知的行索引時為前閉後閉區間,這點與切片稍有不同。
Out[]:
a b c d e
one
two
data.ix[:] #選擇第2到4行,不包括第4行,即前閉後開區間。
Out[]:
a b c d e
two
three
data.ix[-:] #取DataFrame中最後一行,傳回的是DataFrame類型,**注意**這種取法是有使用條件的,隻有當行索引不是數字索引時才可以使用,否則可以選用`data[-1:]`--傳回DataFrame類型或`data.irow(-1)`--傳回Series類型
Out[]:
a b c d e
three
data[-:] #跟上面一樣,取DataFrame中最後一行,傳回的是DataFrame類型
Out[]:
a b c d e
three
data.ix[-] #取DataFrame中最後一行,傳回的是Series類型,這個一樣,行索引不能是數字時才可以使用
Out[]:
a
b
c
d
e
Name: three, dtype: int32
data.tail() #傳回DataFrame中的最後一行
data.head() #傳回DataFrame中的第一行
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
最近處理資料時發現當pd.read_csv()資料時有時候會有讀取到未命名的列,且該列也用不到,一般是索引列被換掉後導緻的,有強迫症的看着難受,這時候dataframe.drop([columns,])是沒法處理的,怎麼辦呢,
最笨的方法是直接給列索引重命名:
data6
Unnamed: high symbol time
date
-- IF1611 ::
-- IF1611 ::
-- IF1611 ::
-- IF1611 ::
-- IF1611 ::
data6.columns = list('abcd')
data6
a b c d
date
-- IF1611 ::
-- IF1611 ::
-- IF1611 ::
-- IF1611 ::
-- IF1611 ::
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
重新命名後就可以用dataframe.drop([columns])來删除了,當然不用我這樣全部給列名替換掉了,可以隻是改變未命名的那個列,然後删除。不過這個用起來總是覺得有點low,有沒有更好的方法呢,有,可以不去删除,直接: