【发布时间】:2018-06-20 02:34:31
【问题描述】:
我有这些列:
index, area, key0
我必须按索引分组(这是一个称为索引的普通列)才能获取具有相同值的行。
#all the ones, all the twos, etc
其中一些(行)是独一无二的。
关于现在不是唯一的:
到目前为止我做了什么:
我必须使用group by 检查哪些组的面积最大,并在名为key1 的新列中将其受人尊敬的key0 值提供给其组中的其他人。
在key0 现在的key1 列中,唯一值仍将具有相同的值
首先,我检查了哪些发生了多次,以便知道哪些将形成组。
df['index'].value_counts()[df['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
345 3
247 3
346 3
347 3
351 3
效果很好。现在的问题是剩下的怎么办?
数据集:
df = pd.DataFrame({"index": [1,2,3,5,1,2,3,3,3], "area":
[50,60,70,80,90,100,10,20,70], "key0": ["1f",2,"3d",4,5,6,7,8,9]})
print df
# INPUT
area index key0
50 1 1f
60 2 2
70 3 3d
80 5 4
90 1 5
100 2 6
10 3 7
20 3 8
70 3 9
import geopandas as gpd
inte=gpd.read_file('in.shp')
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"]])
【问题讨论】:
-
我需要查看部分原始数据框和您的预期输出
-
好吧,数据框和结果就像下面的海报,但我收到一个错误。