将某个大的dataframe进行groupby后,将某列中的n-1个分递增的增加0.1

文章介绍了如何使用Python中的pandas和numpy库对DataFrame进行操作,包括按geom分组、过滤非重要节点(type=0),以及在必要时调整重要节点(type!=0)的坐标。
摘要由CSDN通过智能技术生成
import pandas as pd
import numpy as np

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
d = [10, 21, 10, 21, 21, 10, 23, 23, 25, 25]
f = [0, 1, 1, 2, 0, 0, 2, 2, 0, 0]
df = pd.DataFrame({'id': a, 'geom': d, 'type': f})
#
df = df[['id', 'geom', 'type']]
df_group = df.groupby('geom')
for i, j in df_group:
    print("j:", j)
    unique_values = j['type'].unique()
    # 第一种情况:都是非重要的节点
    if len(unique_values) == 1 and unique_values[0] == 0:
        print("将这个dataframe只保留一条数据")
    else:
        # 1、先将type为节点的数据删除(数据库中删除)
        df_normal = j[j['type'] == 0]
        print("df_normal:", df_normal)

        # 2、判断df_的大小,如果只有一条的不用管,大于一条的修改坐标
        df_ = j[j['type'] != 0]
        print("df_:",df_)
        if len(df_) != 1:
            number = np.concatenate(([0], np.arange(0.1, (df_.shape[0] - 1) * 0.1 + 0.1, 0.1)))
            df_['geom'] = df_['geom'] + number
            print("df_更改后:", df_)

结果

j:    id  geom  type
0   1    10     0
2   3    10     1
5   6    10     0
df_normal:    id  geom  type
0   1    10     0
5   6    10     0
df_:    id  geom  type
2   3    10     1
j:    id  geom  type
1   2    21     1
3   4    21     2
4   5    21     0
df_normal:    id  geom  type
4   5    21     0
df_:    id  geom  type
1   2    21     1
3   4    21     2
df_更改后:    id  geom  type
1   2  21.0     1
3   4  21.1     2
j:    id  geom  type
6   7    23     2
7   8    23     2
df_normal: Empty DataFrame
Columns: [id, geom, type]
Index: []
df_:    id  geom  type
6   7    23     2
7   8    23     2
df_更改后:    id  geom  type
6   7  23.0     2
7   8  23.1     2
j:    id  geom  type
8   9    25     0
9  10    25     0
将这个dataframe只保留一条数据
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值