簡體   English   中英

當某些項目具有相同的 ID 值時,如何從兩個表中獲取所有可能的項目組合?

[英]How do I get all possible item combinations from two tables when some items have the same ID value?

我自學Python,遇到了一個問題。 假設我有兩個 csv 表,列出了項目及其使用的編程語言和框架:

| ProjectID | ProgrammingLanguageID |
| --------- | --------------------- |
| 1         | 32                    |
| 1         | 27                    |
| 3         | 8                     |

和:

| ProjectID | FrameworkID           |
| --------- | --------------------- |
| 1         | 3                     |
| 1         | 7                     |
| 2         | 5                     |

預期的語言框架組合,例如 ProjectID 1 將是:

(p32, f3)
(p32, f7)
(p27, f3)
(p27, f7)

我如何獲得每個項目的所有可能組合?

我認為 itertools 可以解決問題,但我不知道如何包含項目 ID。

您可以將兩個 cdv 都讀取為 pandas 數據幀,然后使用 ProjectID 作為鍵將 FrameworkID 加入 ProgrammingLanguageID。 假設您的第一個 df 被稱為語言,第二個框架被稱為框架,那么下面的方法可以工作

result = languages.merge(frameworks, on="ProjectID", how="outer")

# output
ProjectID   ProgrammingLanguageID   FrameworkID
1           32.0                    3.0
1           32.0                    7.0
1           27.0                    3.0
1           27.0                    7.0
3           8.0                     NaN
2           NaN                     5.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM