使用Geopandas选择.shp文件的特定区域

时间:2017-08-23 01:25:09

标签: python pandas shapefile geopandas

我有一个包含海洋限制的.shp文件。但是,而不是绘制所有这些,我只对6感兴趣.Geopandas创建类似于数据帧的东西(让我们称之为“df”),就像Pandas一样。是否有可能创建一个只有“df”选定区域的新数据帧(“df1”)?

 @IBOutlet weak var Label: UILabel!
@IBAction func ButtonClicked(_ sender: Any) {
    var hourly:Float = 8.00
    var hours:Float = 61.75
    var exemptions:Float = 1.00
    var deductions:Float = 95.80
    var allowances:Float = (exemptions * deductions)
    var gross:Float = (hourly * hours)
    var taxable:Float = (gross - allowances)
    if taxable >= 17529 {
        var fedIncome = ((taxable * 0.396) + 5062.69)}
    else if taxable < 17529 && taxable >= 17458  {
        var fedIncome = ((taxable * 0.35) + 5037.84)}
    else if taxable < 17458 && taxable >= 8081  {
        var fedIncome = ((taxable * 0.33) + 1943.43)}
    else if taxable < 8081 && taxable >= 3925  {
        var fedIncome = ((taxable * 0.28) + 779.75)}
    else if taxable < 3925 && taxable >= 1677  {
        var fedIncome = ((taxable * 0.25) + 217.75)}
    else if taxable < 1677 && taxable >= 484  {
        var fedIncome = ((taxable * 0.15) + 38.80)}
    else if taxable < 484 && taxable >= 96  {
        var fedIncome = (taxable * 0.1)}
    else {
        var fedIncome:Float = 0.0}
     Label.text = ("\(fedIncome)")
}

当我运行时,“tes1”出错:

“系列对象是可变的,因此无法进行哈希处理。”

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

(tes.NAME == "North Pacific Ocean"), (tes.NAME == "South Pacific Ocean")是布尔系列的tuple。您不能将其作为索引器传递。您希望使用按位或|来组合布尔序列,然后使用结果对数据帧进行切片。

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
import geopandas as gp

tes = gp.read_file(r'your\path\World_Seas_IHO_v1\World_Seas.shp')

tes1 = tes[(tes.NAME == "North Pacific Ocean") |
           (tes.NAME == "South Pacific Ocean")]

tes1.plot()

plt.show()
plt.ion()

或者您可以使用isin

tes = gp.read_file(r'your\path\World_Seas_IHO_v1\World_Seas.shp')

tes1 = tes[tes.NAME.isin(['North Pacific Ocean', 'South Pacific Ocean'])]

tes1.plot()

plt.show()
plt.ion()

enter image description here