5

I have these columns: "index", "area_of_poly", "cad_admin" in my GeoDataFrame, the dataset is available here: http://www.mediafire.com/file/x4q5k7xuztq6o3w/p.zip.

Now, I have to group by column called "index" in order to take the rows that have the same value, e.g. all the ones, all the twos, etc.

Some of them (rows) are unique though.

About the ones that are not unique I have to check with a groupby() which of the groups have the largest area and give its respected "CAD_ADMIN" value to the others in its group in a new column called "CAD_ADMIN1".

The unique values are going to still have the same value they had in "CAD_ADMIN" in the now "CAD_ADMIN1" column.

import geopandas as gpd

inte = gpd.read_file('in.shp')

print(inte['index'].value_counts()[inte['index'].value_counts()>1])

359    9
391    8
376    7
374    6
354    5
446    4
403    4
348    4
422    4
424    4
451    4
364    3
315    3
100    3
245    3

inte["rank_gr"] = inte.groupby("index")["area_of_poly"].rank(ascending=False, method="first")

inte["key1_temp"] = inte.apply(lambda row: str(row[""]) if row["rank_gr"] == 1.0 else "", axis = 1)

inte["CAD_ADMIN_FINAL"] = inte.groupby("index")["key1_temp"].transform("sum")

print(inte[["area_of_poly", "index", "CAD_ADMIN", "CAD_ADMIN_FINAL"]])

This code as you will see produces some errors like:

TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

KeyError: ('', 'occurred at index 0')

0

1 Answer 1

4

With such a question you will probably find more luck on the Stack Overflow. However, I may try to resolve it.

There are several hints regarding your code:

  • there are more columns than just "index", "area_of_poly", "cad_admin", so for more easier computation one can create a subset of the original GeoDataFrame with: inte_mini = inte[["index", "area_of_po", "index", "CAD_ADMIN"]].
  • when working with a shapefile keep in mind that the maximum length of field names is 10 characters, so your column "area_of_poly" will be "area_of_po" etc.
  • when creating the "rank_gr" field, one can use the .astype(int) method to convert floats to integers.
  • this str(row[""]) will cause the KeyError: '', because this index "" is meaningless for the pandas.core.series.Series class. Moreover, I do not really understand what are you trying to implement with this statement inte["key1_temp"] = inte.apply(lambda row: str(row[""]) if row["rank_gr"] == 1.0 else "", axis = 1).
  • also it is not clear what is the purpose of this inte['index'].value_counts()[inte['index'].value_counts() > 1] statement. Is it just for visual understanding?

From what I understood in your question, this may be your final code:

import geopandas as gpd

absolute_path_to_shapefile = 'C:/Users/taras/Documents/p/in.shp'

inte = gpd.read_file(absolute_path_to_shapefile)

inte_mini = inte[["index", "area_of_po", "CAD_ADMIN"]]

inte_mini["rank_gr"] = inte_mini.groupby("index")["area_of_po"].rank(ascending=False, method="first").astype(int)

inte_mini_unique = inte_mini[inte_mini["rank_gr"] == 1]

inte_mini = inte_mini.merge(inte_mini_unique[["index", "CAD_ADMIN"]].add_suffix('1'), left_on="index", right_on="index1", how='left')
inte_mini.drop(["index1"], inplace=True, axis=1)

inte_mini["CAD_ADMIN_FINAL"] = inte_mini.groupby("index")["CAD_ADMIN1"].transform("count")

print(inte_mini[["index", "area_of_po", "CAD_ADMIN", "CAD_ADMIN_FINAL"]])

     index     area_of_po CAD_ADMIN  CAD_ADMIN_FINAL
0        2   28047.706672   0113922                1
1       18   78070.668553   0113921                1
2       15  176470.060863   0113921                1
3       27   18303.173073   0113921                1
4       44  149722.831771   0113921                1
..     ...            ...       ...              ...
558    270    1889.449872   0113904                1
559    269    3852.816088   0113904                1
560    272    3796.918474   0113904                1
561    271    7128.637880   0113904                1
562    479   52836.928028   0113927                1

[563 rows x 4 columns]

References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.