简体   繁体   English

如何使一些熊猫列值默认为另一列但同一行中的另一个值?

[英]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.

相关问题 如何为在另一列 pandas 中具有相同值的那些行使一列的值相同 - How to make same value of one column for those rows which have same values in another column pandas 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 如何根据不同列 pandas 中的总和为一行分配一些值? - How to allocate some value to a row on the basis of their sum in different column pandas? 如何用同一行但不同列中的值替换熊猫中的值 - How to replace value in pandas with value in the same row but in a different column 如何从 pandas 中的同一列创建行值数组? - How to make an array of row values from same column in pandas? Pandas:同列不同行如何填写值 - Pandas:How to fill in value from the same column but different row 删除 pandas 列列表中与同一行中的另一列匹配的值 - Remove the values in pandas column list that match another column in that same row 如何使用前一行值以及同一行中其他列中的值来计算pandas中列的值 - how to use previous row value as well as values in other column in same row to compute value of a column in pandas 用另一列中的相同行值替换 pandas dataframe 列中的值 - Replacing values in pandas dataframe column with same row value from another column Pandas Dataframes:列表中列的值是否嵌套在同一行的另一列中? - Pandas Dataframes: is the value of a column in a list nested in another column, same row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM