我有一个这样的数据帧:
Name Object
Patrick ball
Patrick tshirt
Patrick headphones
Leonard ball
Leonard tshirt
Leonard pants
Leonard headphones
Larry ball
Larry headphones
Larry tshirt
Larry cellphone
Maria book
Maria cellphone
Nick computer
Nick ball
objects=['ball','tshirt','headphones','pants','cellphone','book','computer']我想要一个数据帧,其中包含人名和缺少的元素,与对象列表进行比较,如下所示:
Name Object
Patrick pants
Patrick cellphone
Patrick book
Patrick computer
Leonard headphones
Leonard cellphone
Leonard book
Leonard computer
. .
. .
. .
Nick tshit
Nick headphones
Nick pants
Nick cellphone
Nick book发布于 2020-07-03 07:26:44
您可以将pivot_table与any一起用作聚合函数,并将列与objects列表一起使用reindex。使用stack返回序列,但不删除nan (丢失的对象),然后使用isna选择行并创建所需的数据帧。
s = (df.pivot_table(index='Name', columns='Object', aggfunc=any)
.reindex(columns=objects)
.stack(dropna=False)
)
df_ = s[s.isna()].index.to_frame().reset_index(drop=True)
print(df_)
Name Object
0 Patrick pants
1 Patrick cellphone
2 Patrick book
3 Patrick computer
4 Leonard cellphone
5 Leonard book
6 Leonard computer
7 Larry pants
8 Larry book
9 Larry computer
10 Maria ball
11 Maria tshirt
12 Maria headphones
13 Maria pants
14 Maria computer
15 Nick tshirt
16 Nick headphones
17 Nick pants
18 Nick cellphone
19 Nick book要创建s,还可以将set_index和reindex与基于列名的unique元素的MultiIndex.from_product和列表objects一起使用。如果数据帧中有其他列,这种方法会很有趣,这里需要用assign创建一个随机列来执行此操作。
s = (df.assign(a=True)
.set_index(['Name', 'Object'])['a']
.reindex(pd.MultiIndex.from_product([df.Name.unique(), objects],
names=['Name', 'Object']))
)https://stackoverflow.com/questions/62706138
复制相似问题