我有一个产品列表,我想查看其中两种产品的组合使用 Pandas 销售的单个产品的销量。
Date Product Price
1/1/19 Laptop $700
1/1/19 Laptop $700
1/1/19 iPhone $500
1/1/19 Beats $250
1/1/19 iPhone $500
1/2/19 iPhone $500
1/2/19 Beats $250
1/2/19 Laptop $700
1/3/19 Beats $150
1/3/19 Laptop $700
1/3/19 iPhone $500
1/3/19 Beats $250
这只是示例数据,但我想了解 Beats 和 iPhone 在哪些日期获得比笔记本电脑更多的综合收入,以及使用 Pandas 的具体日期。如有任何帮助,我们将不胜感激!
最佳答案
您可以尝试以下方法
>>> df['ProductGroup'] = (df.Product == 'Laptop').replace({True : "Laptop", False : "Iphone & Beats" })
>>> df
Date Product Sale ProductGroup
0 2019-01-01 Laptop 700 Laptop
1 2019-01-01 Laptop 700 Laptop
2 2019-01-01 iPhone 500 Iphone & Beats
3 2019-01-01 Beats 250 Iphone & Beats
4 2019-01-01 iPhone 500 Iphone & Beats
5 2019-01-02 iPhone 500 Iphone & Beats
6 2019-01-02 Beats 250 Iphone & Beats
7 2019-01-02 Laptop 700 Laptop
8 2019-01-03 Beats 150 Iphone & Beats
9 2019-01-03 Laptop 700 Laptop
10 2019-01-03 iPhone 500 Iphone & Beats
11 2019-01-03 Beats 250 Iphone & Beats
<小时/>
>>> df.groupby(['Date', 'ProductGroup']).sum().unstack().droplevel(0, axis=1)
ProductGroup Iphone & Beats Laptop
Date
2019-01-01 1250 1400
2019-01-02 750 700
2019-01-03 900 700
<小时/>
>>> x[x['Iphone & Beats'] > x['Laptop']]
ProductGroup Iphone & Beats Laptop
Date
2019-01-02 750 700
2019-01-03 900 700
关于python - 如何查看 2 个日期产生的收入超过 1 个日期的总收入的实例有多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59456574/