尝试将教程应用于我自己的一些数据。教程链接: https://github.com/brendonhall/facies_classification/blob/master/Facies%20Classification%20-%20SVM.ipynb
我在[3]代码框中。
当试图定义用于绘图的颜色贴图时,我得到错误“TypeError :('list indices必须是整数,而不是float',u'occurred at index 0')”
我在下面粘贴了我的代码。提前谢谢,
import pandas as pd
# Load data
data = pd.read_csv('BHPAND.csv')
data.describe()
data['BH'] = data['BH'].astype('category')
data['FM'] = data['FM'].astype('category')
data['SBTN'] = data['SBTN'].astype('category')
# Set names of Borehole UID's
data['BH'].unique()
# Define colours for facies based on SBTn
# 1=sensitive fine grained 2=organic clay 3=silty clay to clay
# 4=silt mixtures 5=sand mixtures 6=sands 7=gravelly sand
# 8=very stiff sand/clay sand 9=highly overconsolidated fine grained
sbtn_colours = ['#5E5E56', '#1E1E1C','#AAAA96','#788E6D',
'#C6C57D','#FFFC3F', '#AED6F1', '#BA4D2C', '#FF008C']
sbtn_labels = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
#sbtn_colour_map = dictionary that maps sbtn labels to colours
sbtn_colour_map = {}
for ind, label in enumerate(sbtn_labels):
sbtn_colour_map[label] = sbtn_colours[ind]
def label_sbtn(row, labels):
return labels[ row['SBTN'] -1]
data.loc[:,'sbtnLabels'] = data.apply(lambda row: label_sbtn(row, sbtn_labels), axis=1)
检查我的数据类型时,我可以看到SBTN被记录为类别:
data.dtypes
Out[##]:
BH category
MD float64
DR float64
FM category
RHOB float64
M_MOD float64
PHI float64
RES float64
SBTN category
SU float64
VP float64
Z float64
dtype: object
这里的问题是我的列SBTN是一个类别而不是一个int?基本上我想为每个SBTN类型使用我的十六进制颜色定义颜色映射。在原始csv文件中,SBTN是一个整数。
完整错误追溯:
%run "C:\Users\black\Desktop\Pandas Facies\script1.py"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Users\black\Desktop\Pandas Facies\script1.py in <module>()
25 return labels[ row['SBTN'] -1]
26
---> 27 data.loc[:,'sbtnLabels'] = data.apply(lambda row: label_sbtn(row, sbtn_labels), axis=1)
28
29
C:\Users\black\AppData\Local\Enthought\Canopy\User\lib\site-packages\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4150 if reduce is None:
4151 reduce = True
-> 4152 return self._apply_standard(f, axis, reduce=reduce)
4153 else:
4154 return self._apply_broadcast(f, axis)
C:\Users\black\AppData\Local\Enthought\Canopy\User\lib\site-packages\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
4246 try:
4247 for i, v in enumerate(series_gen):
-> 4248 results[i] = func(v)
4249 keys.append(v.name)
4250 except Exception as e:
C:\Users\black\Desktop\Pandas Facies\script1.py in <lambda>(row)
25 return labels[ row['SBTN'] -1]
26
---> 27 data.loc[:,'sbtnLabels'] = data.apply(lambda row: label_sbtn(row, sbtn_labels), axis=1)
28
29
C:\Users\black\Desktop\Pandas Facies\script1.py in label_sbtn(row, labels)
23
24 def label_sbtn(row, labels):
---> 25 return labels[ row['SBTN'] -1]
26
27 data.loc[:,'sbtnLabels'] = data.apply(lambda row: label_sbtn(row, sbtn_labels), axis=1)
TypeError: ('list indices must be integers, not float', u'occurred at index 0')
答案 0 :(得分:0)
错误意味着row['SBTN']
是一个浮点数。将其转换为int
的整数,即labels[int(row['SBTN']) - 1]
。您可能希望确保它实际上是浮点类型中的整数,即assert row['SBTN'] == int(row['SBTN'])
。