這是相關的代碼:
import geopandas as gpd
#A shape file (.shp) is imported here, contents do not matter, since the "size()" function gets the size of the contents
shapefile = 'Data/Code_Specific/ne_50m_admin_1_states_provinces/ne_50m_admin_1_states_provinces.shp'
gdf = gpd.read_file(shapefile)[['admin', 'adm0_a3', 'postal', 'geometry']]
#size
#Return an int representing the number of elements in this object.
print(gdf.size())
我收到最后一行代碼的錯誤,
TypeError: 'numpy.int32' object is not callable
這樣做的主要目的是我試圖整合gdf.size()到一個 for 回圈中:
for index in range(gdf.size()):
print("test", index)
#if Austrailia, remove
if gdf.get('adm0_a3')[index] == "AUS":
gdf = gdf.drop(gdf.index[index])
我完全不知道在這里做什么,這是我在這個網站上的第一篇文章。希望我不會因為這是多么愚蠢或多么簡單而被榮譽徽章所迷惑,我很難過。
uj5u.com熱心網友回復:
gpd.read_file將回傳一個GeoDataFrame或一個DataFrame物件,兩者都具有size回傳整數的屬性。該屬性可以簡單地通過gdf.size在它旁邊添加括號來訪問,你會得到你的錯誤。
size是使用錯誤的屬性,對于表,它回傳行數乘以列數。乍一看,以下應該有效
for index in gdf.index:
...
但是您在迭代時修改了迭代的長度。KeyError如果您洗掉索引并在嘗試訪問它之前,這可能會使所有內容不同步并導致錯誤。由于您要做的只是過濾一些行,只需使用
gdf = gdf[gdf['adm0_a3'] != 'AUS']
uj5u.com熱心網友回復:
我認為你正在尋找的功能是,
gdf.shape[0]
或者
len(gdf.index)
我認為第一個選項更具可讀性,但第二個選項更快。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/355111.html
標籤:Python 麻木的 大熊猫 python-3.9
上一篇:僅當與Python相同年份和月份的值時才應用fillna(method='bfill')
下一篇:通過定義函式構建3D物件
