有没有办法添加或减去现有数据框?
例如,给定以下代码:
import pandas as pd
Apple= pd.DataFrame({'Product':['MacBook', 'iPhone', 'iPad', 'Apple Watch'],
'Inventory':['999', '333', '666', '190']})
有没有办法在现有库存中增加 150 block ,使 Apple Watch 的数量为 340 block ?
请您参考如下方法:
使用 loc 和 bool 选择来更新列:
Apple.loc[Apple.Product.eq('Apple Watch'), 'Inventory'] += 150
Product Inventory
0 MacBook 999
1 iPhone 333
2 iPad 666
3 Apple Watch 340
在您在问题中提供的示例中,Inventory 包含字符串。如果这与您的实际数据相同,则需要先转换为 int:
Apple.Inventory = Apple.Inventory.astype(int)
