英文:
Subtracting values of only matching indexes in Pandas Dataframes
问题
我一直在编写一个使用Pandas管理商店库存的程序。目前我正在做的是从原始库存中减去订购数量,然后打印交易后的库存。我创建了两个数据框来保存原始库存和用户的购物车。
库存
商品 | 价格 | 数量 |
---|---|---|
苹果 | 1.30 | 90 |
香蕉 | 0.57 | 100 |
橙子 | 1.33 | 50 |
书 | 15.0 | 50 |
记事本 | 1.0 | 100 |
购物车
商品 | 成本 | 数量 |
---|---|---|
苹果 | 13.0 | 10 |
橙子 | 6.65 | 5 |
我尝试使用命令 inventory['Quantity'] = inventory['Quantity'] - cart['Quantity']
来获得最终结果,但结果如下:
商品 | 价格 | 数量 |
---|---|---|
苹果 | 1.30 | 80 |
香蕉 | 0.57 | NaN |
橙子 | 1.33 | 45 |
书 | 15.0 | NaN |
记事本 | 1.0 | NaN |
有人知道如何防止其他行显示为NaN吗?
英文:
I've been working on a program to manage a store inventory using Pandas. For what I am working on now is subtracting the quantity ordered from the original and printing the inventory after the transaction. I created two dataframes to hold the original and the user's cart.
Inventory
Items | Price | Quantity |
---|---|---|
Apple | 1.30 | 90 |
Banana | 0.57 | 100 |
Orange | 1.33 | 50 |
Book | 15.0 | 50 |
Notepad | 1.0 | 100 |
Cart
Items | Cost | Quantity |
---|---|---|
Apples | 13.0 | 10 |
Orange | 6.65 | 5 |
I've tried using the command inventory['Quantity'] = inventory['Quantity'] - cart['Quantity']
to get a final result but it comes up as
Items | Price | Quantity |
---|---|---|
Apple | 1.30 | 80 |
Banana | 0.57 | NaN |
Orange | 1.33 | 45 |
Book | 15.0 | NaN |
Notepad | 1.0 | NaN |
Does anyone know how to stop the other rows from coming back as NaN?
答案1
得分: 0
以下是翻译好的部分:
import pandas as pd
# 创建库存数据框
inventory_data = {'Items': ['苹果', '香蕉', '橙子', '书', '记事本'],
'价格': [1.30, 0.57, 1.33, 15.0, 1.0],
'数量': [90, 100, 50, 50, 100]}
inventory = pd.DataFrame(inventory_data)
# 创建购物车数据框
cart_data = {'Items': ['苹果', '橙子'],
'成本': [13.0, 6.65],
'数量': [10, 5]}
cart = pd.DataFrame(cart_data)
# 将物品名称转换为小写以进行匹配
inventory['Items'] = inventory['Items'].str.lower()
cart['Items'] = cart['Items'].str.lower()
# 在“Items”列上合并库存和购物车数据框
merged = pd.merge(inventory, cart, on='Items', how='left')
# 从库存数量中减去购物车数量
merged['数量'] = merged['数量_x'] - merged['数量_y']
# 用库存数量填充NaN值
merged['数量'] = merged['数量'].fillna(merged['数量_x'])
# 选择必要的列并重命名它们
updated_inventory = merged[['Items', '价格', '数量']].rename(columns={'数量': 'Quantity'})
# 将物品名称转换回其原始大小写
updated_inventory['Items'] = updated_inventory['Items'].str.capitalize()
# 将数量列转换为整数
updated_inventory['数量'] = updated_inventory['数量'].astype(int)
# 打印更新后的库存
print(updated_inventory)
希望这对你有所帮助。
英文:
Use the following approach, and you can solve your problem:
import pandas as pd
# Create the inventory data frame
inventory_data = {'Items': ['Apple', 'Banana', 'Orange', 'Book', 'Notepad'],
'Price': [1.30, 0.57, 1.33, 15.0, 1.0],
'Quantity': [90, 100, 50, 50, 100]}
inventory = pd.DataFrame(inventory_data)
# Create the cart data frame
cart_data = {'Items': ['Apples', 'Orange'],
'Cost': [13.0, 6.65],
'Quantity': [10, 5]}
cart = pd.DataFrame(cart_data)
# Convert item names to lowercase for matching
inventory['Items'] = inventory['Items'].str.lower()
cart['Items'] = cart['Items'].str.lower()
# Merge the inventory and cart data frames on the "Items" column
merged = pd.merge(inventory, cart, on='Items', how='left')
# Subtract cart quantities from inventory quantities
merged['Quantity'] = merged['Quantity_x'] - merged['Quantity_y']
# Fill NaN values with inventory quantities
merged['Quantity'] = merged['Quantity'].fillna(merged['Quantity_x'])
# Select the necessary columns and rename them
updated_inventory = merged[['Items', 'Price', 'Quantity']].rename(columns={'Quantity': 'Quantity'})
# Convert the item names back to their original case
updated_inventory['Items'] = updated_inventory['Items'].str.capitalize()
# Convert the quantity column to integers
updated_inventory['Quantity'] = updated_inventory['Quantity'].astype(int)
# Print the updated inventory
print(updated_inventory)
答案2
得分: 0
使用 Series.subtract
,并设置 fill_value = 0
,以便非匹配的索引被减去零。
inventory["Quantity"].subtract(cart["Quantity"], fill_value=0)
英文:
Use Series.subtract
with fill_value = 0
so that non-matched indices are subtracted by zero.
inventory["Quantity"].subtract(cart["Quantity"], fill_value=0)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论