[英]How to make some pandas column values default to another value in a different column, but same row?
I have a sample DataFrame that looks like this: 我有一个示例数据框,看起来像这样:
ID Product UPC Units Sold
no link cereal 3463 12
2211 cereal 2211 13
2211 cereal 8900 11
2211 cereal 6754 14
no link cereal 9012 13
3340 cereal 3340 12
3340 cereal 5436 15
The 'ID' column identifies similar products into one product family ID. “ ID”列将相似的产品标识为一个产品系列ID。 The ID is created by the first UPC number of that family.
该ID由该系列的第一个UPC号创建。 'No link' identifies products that are the only member of their family.
“无链接”表示产品是其家族的唯一成员。 What I want is set the 'no link' values to default to the UPC number.
我想要的是将“无链接”值设置为默认为UPC编号。 This is what I want my output to look like:
这是我希望输出看起来像的样子:
ID Product UPC Units Sold
3463 cereal 3463 12
2211 cereal 2211 13
2211 cereal 8900 11
2211 cereal 6754 14
9012 cereal 9012 13
3340 cereal 3340 12
3340 cereal 5436 15
This is what I have so far: 这是我到目前为止的内容:
for row in product_families:
if product_families.loc['Product Family Number'] == 'no link':
Use loc
with boolean indexing and let Pandas assign with intrinsic data alignment: 将
loc
与布尔索引一起使用,并让Pandas进行内部数据对齐:
df.loc[df.ID.eq('no link'),'ID'] = df.UPC
Output: 输出:
ID Product UPC Units Sold
0 3463 cereal 3463 12
1 2211 cereal 2211 13
2 2211 cereal 8900 11
3 2211 cereal 6754 14
4 9012 cereal 9012 13
5 3340 cereal 3340 12
6 3340 cereal 5436 15
Scott Boston's solution should work. 斯科特·波士顿的解决方案应该起作用。
Maybe worth trying a different approach using apply row wise. 也许值得尝试使用逐行应用另一种方法。
df['ID']=df.apply(lambda x: x.UPC if x.ID=='no link' else x.ID, axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.