设置set_index后,Simplekml无法读取数据帧

时间:2018-08-06 14:57:45

标签: python pandas simplekml

在如下所示的pandas数据帧上应用simplekml效果很好:

>>>df.head(2)
                file_name          Longitude  Latitude  Altitude Pitch  \
0  20180730_144008_577_8b.JPG  -147.858566284444  64.85537   151.099     0   
1  20180730_144009_588_8b.JPG  -147.858566284444  64.85537   150.913     0   

然后运行simplekml:

>>>  kml=simplekml.Kml()
     df.apply(lambda X: kml.newpoint(name=X["file_name"], 
     X["Longitude"],X["Latitude"])]), axis=1)
     kml.save('file.kml')

一切都很好,没有问题。 当我设置索引并尝试再次运行代码以创建kml文件时,会发生问题:

>>>df.set_index('file_name',inplce=True)
>>>df.head(2)
                                Longitude  Latitude  Altitude Pitch Roll
file_name                                                                      
20180730_144008_577_8b.JPG  -147.858566284444  64.85537   151.099     0       
20180730_144009_588_8b.JPG  -147.858566284444  64.85537   150.913     0   

>>>df.apply(lambda X: kml.newpoint(name=X["file_name"],coords=[(   X["Longitude"],X["Latitude"])]), axis=1)
Traceback (most recent call last):
  File "/home/esaiet/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2563, in get_value
    return libts.get_value_box(s, key)
  File "pandas/_libs/tslib.pyx", line 1018, in pandas._libs.tslib.get_value_box
  File "pandas/_libs/tslib.pyx", line 1026, in pandas._libs.tslib.get_value_box
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/idlelib/pyshell.py", line 762, in runcode
    exec(code, self.locals)
  File "<pyshell#49>", line 1, in <module>
  File "/home/esaiet/.local/lib/python3.6/site-packages/pandas/core/frame.py", line 4854, in apply
    ignore_failures=ignore_failures)
  File "/home/esaiet/.local/lib/python3.6/site-packages/pandas/core/frame.py", line 4950, in _apply_standard
    results[i] = func(v)
  File "<pyshell#49>", line 1, in <lambda>
  File "/home/esaiet/.local/lib/python3.6/site-packages/pandas/core/series.py", line 623, in __getitem__
    result = self.index.get_value(self, key)
  File "/home/esaiet/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2571, in get_value
    raise e1
  File "/home/esaiet/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2557, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas/_libs/index.pyx", line 83, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 91, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('file_name', 'occurred at index 20180730_144008_577_8b.JPG')

为什么设置索引时会出现此错误? 我知道如果重置索引,simplekml将再次起作用:

>>>df.reset_index(inplace=True)
>>>df.head()
                file_name          Longitude   Latitude  Altitude Pitch  \
0  20180730_144008_577_8b.JPG  -147.858566284444  64.855370   151.099     0   
1  20180730_144009_588_8b.JPG  -147.858566284444  64.855370   150.913     0   
>>>df.apply(lambda X: kml.newpoint(name=X["file_name"],coords=[( X["Longitude"],X["Latitude"])]), axis=1)

简单的kml运行正常。引起问题的熊猫数据框的变化是什么?

0 个答案:

没有答案