在groupby中一列缺少某个值的地方插入行

2024-05-12 19:42:14 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的数据帧:

user1    user2    cat    quantity + other quantities
----------------------------------------------------
Alice    Bob      0      ....
Alice    Bob      1      ....
Alice    Bob      2      ....
Alice    Carol    0      ....
Alice    Carol    2      ....

我想确保每个user1-user2对都有一行对应于每个类别(有三个:0,1,2)。如果不是,我想插入一行,并将其他列设置为零。你知道吗

user1    user2    cat    quantity + other quantities
----------------------------------------------------
Alice    Bob      0      ....
Alice    Bob      1      ....
Alice    Bob      2      ....
Alice    Carol    0      ....
Alice    Carol    1      <SET ALL TO ZERO>
Alice    Carol    2      ....

到目前为止,我得到的是所有user1-user2的列表,其中cat的值少于3个:

df.groupby(['user1','user2']).agg({'cat':'count'}).reset_index()[['user1','user2']]

我可以迭代这些用户,但这需要很长时间(有超过100万对这样的用户)。我已经检查过基于某些条件(如Pandas/Python adding row based on conditionInsert row in Pandas Dataframe based on a condition)在pandas中插入行的其他解决方案,但它们并不完全相同。你知道吗

此外,由于这是一个巨大的数据集,解决方案必须矢量化。我该怎么办?你知道吗


Tags: 数据用户pandasonquantitycatbasedrow
1条回答
网友
1楼 · 发布于 2024-05-12 19:42:14

通过^{}^{}^{}一起使用:

print (df)
   user1  user2  cat  quantity  a
0  Alice    Bob    0         2  4
1  Alice    Bob    1         3  4
2  Alice    Bob    2         4  4
3  Alice  Carol    0         6  4
4  Alice  Carol    2         3  4

df = df.set_index(['user1','user2', 'cat'])
mux = pd.MultiIndex.from_product(df.index.levels, names=df.index.names) 
df = df.reindex(mux, fill_value=0).reset_index()
print (df)
   user1  user2  cat  quantity  a
0  Alice    Bob    0         2  4
1  Alice    Bob    1         3  4
2  Alice    Bob    2         4  4
3  Alice  Carol    0         6  4
4  Alice  Carol    1         0  0
5  Alice  Carol    2         3  4

另一种解决方案是通过列的唯一值和带有right连接的merge的所有组合来创建新的Dataframe

from  itertools import product

df1 = pd.DataFrame(list(product(df['user1'].unique(),
                                df['user2'].unique(),
                                df['cat'].unique())), columns=['user1','user2', 'cat'])
df = df.merge(df1, how='right').fillna(0)
print (df)
   user1  user2  cat  quantity    a
0  Alice    Bob    0       2.0  4.0
1  Alice    Bob    1       3.0  4.0
2  Alice    Bob    2       4.0  4.0
3  Alice  Carol    0       6.0  4.0
4  Alice  Carol    2       3.0  4.0
5  Alice  Carol    1       0.0  0.0

编辑2:

df['user1'] = df['user1'] + '_' + df['user2']
df = df.set_index(['user1', 'cat']).drop('user2', 1)
mux = pd.MultiIndex.from_product(df.index.levels, names=df.index.names)
df = df.reindex(mux, fill_value=0).reset_index()
df[['user1','user2']] = df['user1'].str.split('_', expand=True)
print (df)
   user1  cat  quantity  a  user2
0  Alice    0         2  4    Bob
1  Alice    1         3  4    Bob
2  Alice    2         4  4    Bob
3  Alice    0         6  4  Carol
4  Alice    1         0  0  Carol
5  Alice    2         3  4  Carol

编辑3:

cols = df.columns.difference(['user1','user2'])
df = (df.groupby(['user1','user2'])[cols]
        .apply(lambda x: x.set_index('cat').reindex(df['cat'].unique(), fill_value=0))
        .reset_index())
print (df)
   user1  user2  cat  a  quantity
0  Alice    Bob    0  4         2
1  Alice    Bob    1  4         3
2  Alice    Bob    2  4         4
3  Alice  Carol    0  4         6
4  Alice  Carol    1  0         0
5  Alice  Carol    2  4         3

相关问题 更多 >