1

I am working with geopandas

There are these columns:geo,cat and I have to make the new column called rank.

rank should contain according to the groups with the similar cat number an increasing number according to the (about to be sorted) x coordinates which will be extracted from the geo column to a new column that will only contain the x coordinates.

  geo
  POINT (270504.6944782521 4277096.25338444)
  POINT (270838.1885699595 4278145.324786565)
  POINT (270606.3947049045 4277995.041739198)
  POINT (271508.653647932 4278548.893014569)

It will look like that

    cat   rank sorted_x
0   100   1    #here will be the westernmost point of the values where cat is 100 and will proceed to the easternmost. Then the same for the 101 values in cat and so on.
1   100   2
2   100   3
3   101   1
4   101   2
5   102   1
6   102   2
7   103   1
8   103   2
9   103   3
10  103   4
11  104   1
12  104   2  

To sum up: extract x coords from the geo to another new column then group by their cat column and do the ranking on a new rank column with a pattern (for example from west to east) based on the x coordinates(which will then be sorted in order to have this output) of each group.

Here is what I have done already to achieve this result:

df['rank'] = df.groupby('cat').cumcount()+1.astype(str).str.zfill(2)




   cat   rank
0   100   01
1   100   02
2   100   03
3   101   01
4   101   02
5   102   01
6   102   02
7   103   01
8   103   02
9   103   03
10  103   04
11  104   01
12  104   02

The problem is that it doesn't do it based on the x coordinates but I think it should help to pass the logic of what I want to do.

  • Is your question basically how to get a column of x coordinates? – joris Jun 13 '18 at 12:58
  • There is the element of sorting between groups of the x coordinates and doing the ranking accordingly, so not only that . – user122244 Jun 13 '18 at 13:02
1
import geopandas as gpd
import pandas as pd

#Create some data:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world['centroid_column'] = world.centroid
world = world.set_geometry('centroid_column')
world.drop(['pop_est', 'name', 'iso_a3', 'gdp_md_est','geometry'], axis=1, inplace=True)

#Create an x coordinate column
world['x'] = world['centroid_column'].apply(lambda p: p.x)

#Set x column as index and sort:
world = world.set_index('x').sort_index()

world['rank'] = (world.groupby('continent').cumcount()+1).astype(str).str.zfill(2)

First rows in data frame:

x                   continent       centroid_column                                rank
-112.599438377327   North America   POINT (-112.5994383773273 45.70562953540318)    01
-102.576349523987   North America   POINT (-102.5763495239869 23.93537190224483)    02
-98.1423813720972   North America   POINT (-98.14238137209725 61.46907614534906)    03
-90.3694583605315   North America   POINT (-90.36945836053152 15.6993606120269)     04
-88.8729031703238   North America   POINT (-88.87290317032378 13.72609162579419)    05
-88.7034212529932   North America   POINT (-88.70342125299317 17.19708991145154)    06
-86.5899638380155   North America   POINT (-86.58996383801548 14.82294708165294)    07
-85.0203185008025   North America   POINT (-85.02031850080247 12.84819042803697)    08
-84.1754230960095   North America   POINT (-84.17542309600947 9.965671127464525)    09
-80.1091648354938   North America   POINT (-80.10916483549381 8.530019388864654)    10
-78.9606849097026   North America   POINT (-78.96068490970256 21.63175154102523)    11
-78.3841667460837   South America   POINT (-78.38416674608372 -1.45477170554058)    01
-77.9299708039351   North America   POINT (-77.92997080393509 25.51549172533655)    12
-77.3242548016489   North America   POINT (-77.32425480164892 18.13763612786844)    13
-74.3918058168472   South America   POINT (-74.39180581684721 -9.19156290513455)    02
-73.0777320869748   South America   POINT (-73.07773208697481 3.927213862709704)    03
-72.6580133053558   North America   POINT (-72.65801330535575 18.90070069184333)    14

See:

Access x and y coordinates of Point geoseries through attributes

and

cumsum per group in column ordered by second column append to original dataframe

  • I am trying to label the x coordinates with this:ax = df.plot() for x ,label in zip(df.x,df.x): ax.annotate(label, x=(x), xtext=(3, 3), textcoords="offset points") and although I am not righting correct, I also have problem with the x because it is an index now and not considered a column. – user122244 Jun 14 '18 at 9:17
  • I dont know how to fix your labels. To fix index: df.reset_index(inplace=True) – BERA Jun 14 '18 at 9:24
  • Will it affect the previous sorting situation ? – user122244 Jun 14 '18 at 9:25
  • Dont think so, try it and see – BERA Jun 14 '18 at 9:29
  • It is fine. The reason you set it as an index was because it is the only way to do the sort? – user122244 Jun 14 '18 at 9:39

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.