我目前正在开发一个d3树映射,它需要一个嵌套的json作为条目,我成功地组织了我的df并生成了json,但是我的一些树映射矩形比其他的大30倍,所以我决定删除生成这个矩形的行。你知道吗
我的函数dropSmall()
在我的列和行中迭代,以验证每个groupby的和是否比最大和小30倍
我正在努力更新df,要么使用drop,要么影响匹配的值
这是我的密码:
def dropSmall(df):
list = []
for i in df.columns: #b, c, z ..
if i != 'valeur' and i!='unite':
list.append(i)
# iterating on rows
for j in range(df.groupby(list).sum().shape[0]):
myMax = df.groupby(list).sum().iloc[:, 0].max() / 30
myJ = df.groupby(list).sum().iloc[:, 0][j]
myDf = df.groupby(list).sum().iloc[:, 0]
if myJ <= myMax:
df = df[myDf['value']>= myMax]
我的团员是这样的
name b c z l sL value unit
3099 Myindicator 1 1 3 NA NA 129.74 kg
3100 1 44929.74 kg
3101 2 5174.74 kg
3110 3 1 3 1 NA 2497.66 kg
3156 2 NA 29.43 kg
3222 3 NA 304.81 kg
对于第一行的例子,当b=1 c=1 z=3 l=NA时,我想在迭代3 sL时验证sL的值是该和的最大值的30倍,对于这种情况,当value=129时删除该行
我的函数验证条件,但我不知道如何从初始df notdf.groupby('list').sum()
中删除行
第一行的未分组df示例
name Continent Region Country State City Borough Value Unit
1000 Myindicator 1 1 3 1 1 1 53.86 kg
[从此处编辑]
我的截止乘数是2 每个层次都有一个最大值
Value
name Continent Region Country State
Myindicator 1 1 1 7 50[MAX]
8 30
2 5 70[MAX]
6 30 *
3 1 50[MAX]
4 5 200[MAX]
6 150
5 1 300[MAX]
6 160
7 100*
8 50*
9 50*
2 4 9 100[MAX]
10 40 *
5 3 80[MAX]
11 20 *
6 2 10[MAX]
3 7 12 100[MAX]
在本例中,您不会删除region 2 country 6 state 2,因为它是此region>;country>;state的唯一行,同时也是最大值
希望这更清楚
所以我不是100%清楚你的输入是什么样子的,或者你想要什么回来,但是如果我理解正确的话,我认为下面的方法是可行的。你知道吗
从这里编辑
EDIT2:添加星号(
*
)以指示要删除的行。你知道吗EDIT3:由于赋值和副本处理
pandas.DataFrame
的方式而更改了函数执行此过程的函数:
下面是示例表的工作方式。你知道吗
分组在
['name']
此层次结构的分组总和和截止值:删除的索引: 没有
分组在
['name', 'Continent']
此层次结构的分组总和和截止值:删除的索引: 没有
分组在
['name', 'Continent', 'Region']
此层次结构的分组总和和截止值:删除的索引: 20,21岁
分组在
['name', 'Continent', 'Region', 'Country']
此层次结构的分组总和和截止值:删除的索引: 13、16、17、18、19岁
分组在
['name', 'Continent', 'Region', 'Country', 'State']
此层次结构的分组总和和截止值:删除的索引: 4、6、9、10
最终表格:
相关问题 更多 >
编程相关推荐