python - 在 Pandas 数据框中为每个组插入缺失的类别

标签 python pandas data-manipulation

我需要为每个组插入缺失的类别,这是一个示例:

import pandas as pd
import numpy as np

df = pd.DataFrame({ "group":[1,1,1 ,2,2],
                   "cat": ['a', 'b', 'c', 'a', 'c'] ,
                   "value": range(5),
                   "value2": np.array(range(5))* 2})

df

# test dataframe

cat group   value value2
a   1         0   0
b   1         1    2
c   1         2    4
a   2         3    6
c   2         4    8

假设我有一些 categories = ['a', 'b', 'c', 'd']。如果 cat 列 不包含列表中的类别,我想插入 一行,每个组的值为 0。 category如何每组插入一行,从而得到每组的所有类别

cat group   value  value2
a   1         0    0
b   1         1    2
c   1         2    4
d   1         0    0
a   2         3    6
c   2         4    8
b   2         0    0
d   2         0    0

最佳答案

groupby这里不是必须的,只需要reindex即可通过 MultiIndex:

categories = ['a', 'b', 'c', 'd']
mux = pd.MultiIndex.from_product([df['group'].unique(), categories], names=('group','cat'))
df = df.set_index(['group','cat']).reindex(mux, fill_value=0).swaplevel(0,1).reset_index()
print (df)
  cat  group  value  value2
0   a      1      0       0
1   b      1      1       2
2   c      1      2       4
3   d      1      0       0
4   a      2      3       6
5   b      2      0       0
6   c      2      4       8
7   d      2      0       0

有很多解决方案,所以我添加timings:

np.random.seed(123)
N = 1000000
L = list('abcd') #235,94.1,156ms

df = pd.DataFrame({'cat': np.random.choice(L, N, p=(0.002,0.002,0.005, 0.991)),
                   'group':np.random.randint(10000,size=N),
                   'value':np.random.randint(1000,size=N),
                   'value2':np.random.randint(5000,size=N)})
df = df.sort_values(['group','cat']).drop_duplicates(['group','cat']).reset_index(drop=True)
print (df.head(10))

categories = ['a', 'b', 'c', 'd']

def jez(df):

    mux = pd.MultiIndex.from_product([df['group'].unique(), categories], names=('group','cat'))
    return df.set_index(['group','cat']).reindex(mux, fill_value=0).swaplevel(0,1).reset_index()

def f(x):
    return x.reindex(categories, fill_value=0).assign(group=x['group'][0].item())

def coldspeed(df):
    return df.set_index('cat').groupby('group', group_keys=False).apply(f).reset_index()    

def zero(df):
    from itertools import product
    dfo = pd.DataFrame(list(product(df['group'].unique(), categories)),
                            columns=['group', 'cat'])
    return dfo.merge(df, how='left').fillna(0)

def wen(df):
    return df.groupby('group',as_index=False).apply(lambda x : x.set_index('cat').reindex(categories)).fillna(0).reset_index().drop('level_0',1)

def bharath(df):
    mux = pd.MultiIndex.from_product([df['group'].unique(), categories], names=('group','cat'))
    return mux.to_frame().merge(df,on=['cat','group'],how='outer').fillna(0)

def akilat90(df):
    grouped = df.groupby('group')
    categories =  pd.DataFrame(['a', 'b', 'c', 'd'], columns=['cat'])
    merged_list = []
    for g in grouped:
        merged = pd.merge(categories, g[1], how = 'outer', on='cat')
        merged['group'].fillna(merged['group'].mode()[0],inplace=True) # replace the `group` column's `NA`s by mode
        merged.fillna(0, inplace=True)
        merged_list.append(merged)

    return pd.concat(merged_list)


print (jez(df))
print (coldspeed(df))
print (zero(df))
print (wen(df))
print (bharath(df))
print (akilat90(df))

In [262]: %timeit (jez(df))
100 loops, best of 3: 11.5 ms per loop

In [263]: %timeit (bharath(df))
100 loops, best of 3: 16 ms per loop

In [264]: %timeit (zero(df))
10 loops, best of 3: 28.3 ms per loop

In [265]: %timeit (wen(df))
1 loop, best of 3: 8.74 s per loop

In [266]: %timeit (coldspeed(df))
1 loop, best of 3: 8.2 s per loop

In [297]: %timeit (akilat90(df))
1 loop, best of 3: 23.6 s per loop

关于python - 在 Pandas 数据框中为每个组插入缺失的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117982/

相关文章:

python - 如何从新闻摘要中提取股票代码NUMBER?

python - 将年份和日期转换为 Pandas 中的日期时间索引

python - 如何使用 .query() 通过时间戳函数过滤 Pandas 数据帧

python - 如何更新 Tkinter Canvas 上的图像?

python - 将多索引/多级数据框减少为单索引、单级

python - 如果写入字符串变量,AG34970a device.write 的 PyVisa 控制会导致错误

python - 获取重复的行和文件的其余部分 [大文件 50G]

pandas - 如何在单元格顶部显示文本

Python- Pandas : number/index of the minimum value in the given row

python - 如何根据重复行数提取数据?