gpt4 book ai didi

python - 在 pandas 中插入行,其中一列缺少 groupby 中的某些值

转载 作者:太空宇宙 更新时间:2023-11-04 02:25:14 25 4
gpt4 key购买 nike

这是我的数据框:

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 中插入行的解决方案,但它们并不完全相同。

此外,由于这是一个庞大的数据集,因此必须对解决方案进行矢量化处理。我应该如何进行?

最佳答案

使用set_indexreindex通过 MultiIndex.from_product :

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

另一种解决方案是通过列的唯一值的所有组合创建新的 Dataframe 并使用 right join merge:

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

关于python - 在 pandas 中插入行,其中一列缺少 groupby 中的某些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com