我有一个数据表,其中公司有不同数量的位置,每个键有不同的值。
| 关键词|公司|价值|位置|
| - ------|- ------|- ------|- ------|
| 开始|汉堡包|星期一|雪梨|
| 开始|汉堡包|星期日|布里斯班|
| 开始|甜甜圈|星期一|雪梨|
我希望计算不同的值,但要进行标准化,以便具有多个位置的公司不会扭曲事情。我希望的输出如下所示:
| 价值|计数|
| - ------|- ------|
| 星期一|1.5岁|
| 星期日|0.5分|
不断敲打我的头对如何处理这一点,所以任何帮助是非常感谢。
1条答案
按热度按时间iecba09b1#
我觉得你在找这样的东西:
完整示例
下面是完整的示例,包括导入、问题中包含的示例数据以及关于每个步骤的一些注解:
使用方法链的解决方案
如果您不想创建中间的Pandas数据框,如
df1
,您可以将所有步骤组合成一个命令链,如下所示:pandas.DataFrame.assign
允许您引用链中上一步的列。例如,在创建Location_Rate
列时,第二个.assign
引用Total_Locations
。