重新映射和重新组合python pandas中的值

我有一个数据框,其中值已分配给组:


import pandas as pd


df = pd.DataFrame({ 'num' : [0.43, 5.2, 1.3, 0.33, .74, .5, .2, .12],

                   'group' : [1, 2, 2, 2, 3,4,5,5]

                    })


df


  group num

0   1   0.43

1   2   5.20

2   2   1.30

3   2   0.33

4   3   0.74

5   4   0.50

6   5   0.20

7   5   0.12

我想确保没有价值在一个小组中。如果值为“孤立”,则应将其重新分配给成员多于一个的下一个最高组。因此,结果数据框应如下所示:


  group num

0   2   0.43

1   2   5.20

2   2   1.30

3   2   0.33

4   5   0.74

5   5   0.50

6   5   0.20

7   5   0.12

实现此结果的最有效方法是什么?


德玛西亚99
浏览 126回答 2
2回答

当年话下

只能将向量化操作用于此任务。您可以pd.Series.bfill用来创建从原始索引到新索引的映射:counts = df['group'].value_counts().sort_index().reset_index()counts['original'] = counts['index']counts.loc[counts['group'] == 1, 'index'] = np.nancounts['index'] = counts['index'].bfill().astype(int)print(counts)   index  group  original0      2      1         11      2      3         22      5      1         33      5      1         44      5      2         5然后使用pd.Series.map执行映射:df['group'] = df['group'].map(counts.set_index('original')['index'])print(df)   group   num0      2  0.431      2  5.202      2  1.303      2  0.334      5  0.745      5  0.506      5  0.207      5  0.12

慕田峪7331174

这是我发现的一种解决方案,可能有更好的方法来执行此操作...# Find the orphanscount = df.group.value_counts().sort_index()orphans = count[count == 1].index.values.tolist()# Find the setssets = count[count > 1].index.values.tolist()# Find where orphans should be remappedwhere = [bisect.bisect(sets, x) for x in orphans]remap = [sets[x] for x in where]# Create a dictionary for remapping, and replace original valueschange = dict(zip(orphans, remap))df = df.replace({'group': change})df  group num0   2   0.431   2   5.202   2   1.303   2   0.334   5   0.745   5   0.506   5   0.207   5   0.12
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python