Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在熊猫或excel中建立关系?_Python_Excel_Pandas - Fatal编程技术网

Python 如何在熊猫或excel中建立关系?

Python 如何在熊猫或excel中建立关系?,python,excel,pandas,Python,Excel,Pandas,我正在处理一个问题,我想计算发生的次数。我有一张包含客户数据的excel表格。我发现人们带来了类似的产品。让我们假设有人带了一部新手机,大多数时候他们都带了一个电池组。这两个订单的订单id将相同。我如何计算发生的次数来推断,如果人们购买一种产品,他们很可能会购买另一种 输入: ----------------------------------------------| order id | Name | product | ----------|----

我正在处理一个问题,我想计算发生的次数。我有一张包含客户数据的excel表格。我发现人们带来了类似的产品。让我们假设有人带了一部新手机,大多数时候他们都带了一个电池组。这两个订单的订单id将相同。我如何计算发生的次数来推断,如果人们购买一种产品,他们很可能会购买另一种

输入:

----------------------------------------------|
order id  | Name        |    product          |
----------|-------------|---------------------|
123456    | Will Smith  |   mobile phone      |
123456    | Will Smith  |   power bank        |
123456    | Will Smith  |   charger           |
234567    | adam smith  |   mobile phone      |
234567    | adam smith  |   power bank        |
345678    | sam smith   |   mobile phone      |
345678    | sam smith   |   charger           |
345678    | sam smith   |   headphone         |
345672    | Ash smith   |   mobile phone      |
345672    | Ash smith   |   charger           |
345673    | kim smith   |   headphone         |   
----------------------------------------------|
预期产量

----------------------------------------------|
order id  | Name        |    product          |
----------|-------------|---------------------|
123456    | Will Smith  |   mobile phone      |
123456    | Will Smith  |   power bank        |
123456    | Will Smith  |   charger           |
234567    | adam smith  |   mobile phone      |
234567    | adam smith  |   power bank        |

输出应包含移动电话和电源组。

您可以使用
groupby
agg(['count'])
来计算行值组的出现次数,例如:

#!/usr/bin/env python

import io
import pandas as pd

table_str = '''Order_id, Name, Product
123456, Will Smith, mobile phone
123456, Will Smith, power bank
123456, Will Smith, charger
234567, adam smith, mobile phone
234567, adam smith, power bank
345678, sam smith, mobile phone
345678, sam smith, charger
345678, sam smith, power bank
345678, sam smith, headphone
'''

def main():
    df = pd.read_csv(io.StringIO(table_str), header=0, skipinitialspace=True)
    df = df.groupby(['Order_id', 'Name']).agg(['count'])
    print(df)

if __name__ == '__main__':
    main()
结果:

$ ./so67644922.py
                    Product
                      count
Order_id Name              
123456   Will Smith       3
234567   adam smith       2
345678   sam smith        4
这将显示唯一订单和用户之间的关系以及他们购买的产品数量。可以调整列名以查看其他关系

您可以从Excel导出CSV文件,并通过相同的
read\u CSV()
函数将该文件读取到数据框中


注意:以后提问时,请不要发布图像来表示您的数据。发布一个文本格式的表格,可以更容易地复制和粘贴。这将有助于您将来获得帮助。

在Excel 365中,它将如下所示:

=FILTER(A2:B100,COUNTIFS(A2:A100,A2:A100,B2:B100,"mobile phone")*COUNTIFS(A2:A100,A2:A100,B2:B100,"power bank"))

欢迎来到堆栈溢出。请不要包含数据的图像。始终将数据放在文本格式中,这样就可以复制/粘贴数据,或者更好地发布创建输入数据的代码。通过这种方式,人们实际上能够使用这些数据来尝试和再现您的问题。还请包括您的预期/期望输出,并解释您需要输出的格式(熊猫数据框等)。感谢Alex Reynolds,很抱歉以这种方式发布问题,因为我是stackoverflow的新手。我的意思是,我想以这样一种方式进行排序,即一列包含订单id,相邻列包含手机和电源银行以及其他产品(如果存在订单id)。而不包含手机和电源银行的订单id将不会在查询中填充。提供输入和预期输出。不要使用图像。使用文本。这是给你一个很好的机会得到你想要的答案的最好方法。请看一下输入和预期输出。不客气。如果答案对你有用,请接受/投票。