基于关联的值将值连接到多个列中

2024-10-02 14:18:18 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个数据帧

+----+-------+------+-----------+-----------+---------------+
|    |   Key | ID   | Status1   | Status2   | OrderID       |
|----+-------+------+-----------+-----------+---------------|
|  0 |     1 | A1   | False     | True      | 1234-USF-0025 |
|  1 |     1 | A1   | False     | True      | 1234-USF-0026 |
|  2 |     1 | A1   | False     | True      | 1234-USF-0027 |
|  3 |     2 | A1   | True      | True      | 1234-USF-0025 |
|  4 |     2 | A1   | True      | True      | 1234-USF-0026 |
|  5 |     2 | A1   | True      | True      | 1234-USF-0027 |
|  6 |     3 | A1   | Anything  | True      | 1234-USF-0025 |
|  7 |     3 | A1   | False     | True      | 1234-USF-0026 |
|  8 |     3 | A1   | False     | Anything  | 1234-USF-0027 |
|  9 |     4 | A2   | True      | True      | 1234-USF-0028 |
| 10 |     4 | A2   | True      | True      | 1234-USF-0029 |
| 11 |     4 | A2   | True      | True      | 1234-USF-0030 |
| 12 |     5 | A3   | True      | True      | 1234-USF-0031 |
| 13 |     5 | A3   | True      | True      | 1234-USF-0032 |
| 14 |     5 | A3   | True      | True      | 1234-USF-0033 |
| 15 |     6 | A4   | True      | True      | 1234-USF-0034 |
| 16 |     6 | A4   | True      | True      | 1234-USF-0035 |
| 17 |     6 | A4   | True      | True      | 1234-USF-0036 |
+----+-------+------+-----------+-----------+---------------+

如何转换为按ID列出每个OrderID,并基于每个Status连接Key。如果两个Stautses都是真的,那么连接的Keys应该放在TRUE列中。如果其中一个是FlaseKeys应该放在FALSE列中。如果其中一个(或两者)Status不是TrueFalse,则Key(s)会连接到Other列中

期望结果df

Order ID        ID  TRUE    FALSE  OTHER
1234-USF-0025   A1   2       1       3
1234-USF-0026   A1   2       1,3
1234-USF-0027   A1   2       1       3
1234-USF-0028   A2   4  
1234-USF-0029   A2   4  
1234-USF-0030   A2   4  
1234-USF-0031   A3   5  
1234-USF-0032   A3   5  
1234-USF-0033   A3   5  
1234-USF-0034   A4   6  
1234-USF-0035   A4   6  
1234-USF-0036   A4   6  

我尝试过的

df = df.groupby(['OrderID','ID'])['Key'].apply(','.join).reset_index()

+----+---------------+------+-------+
|    | OrderID       | ID   | Key   |
|----+---------------+------+-------|
|  0 | 1234-USF-0025 | A1   | 1,2,3 |
|  1 | 1234-USF-0026 | A1   | 1,2,3 |
|  2 | 1234-USF-0027 | A1   | 1,2,3 |
|  3 | 1234-USF-0028 | A2   | 4     |
|  4 | 1234-USF-0029 | A2   | 4     |
|  5 | 1234-USF-0030 | A2   | 4     |
|  6 | 1234-USF-0031 | A3   | 5     |
|  7 | 1234-USF-0032 | A3   | 5     |
|  8 | 1234-USF-0033 | A3   | 5     |
|  9 | 1234-USF-0034 | A4   | 6     |
| 10 | 1234-USF-0035 | A4   | 6     |
| 11 | 1234-USF-0036 | A4   | 6     |
+----+---------------+------+-------+

上面的内容当然让我很接近,但我不知道如何将Keys分解成各自的列(TRUEFALSEOTHER

注意事项

我以前将Key列转换为字符串

Order IDs可以为IDs复制,但将具有不同的Keys


Tags: keyidfalsetruea2dfa1keys
1条回答
网友
1楼 · 发布于 2024-10-02 14:18:18

这是一个可行的解决方案,但肯定有一个更快更干净的方法。首先为布尔逻辑添加一列,然后执行groupby压缩表,然后使用KeyResult列遍历并填充TrueFalseOther列。最后,我删除不需要的列并聚合行

import pandas as pd
import numpy as np
# Your dataframe for testing purposes
df = pd.DataFrame({'Key': '1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6'.split(),
                   'ID': 'A1 A1 A1 A1 A1 A1 A1 A1 A1 A2 A2 A2 A3 A3 A3 A4 A4 A4'.split(),
                   'Status1': 'False False False True True True Anything False False True True True True True True True True True'.split(),
                   'Status2': 'True True True True True True True True Anything True True True True True True True True True'.split(),
                   'OrderID': '25 26 27 25 26 27 25 26 27 28 29 30 31 32 33 34 35 36'.split()})



# First we need to do this boolean logic
df["Result"] = ""
for index, row in df.iterrows():
  stat1 = row["Status1"]
  stat2 = row["Status2"]

  if stat1 == "True" and stat2 == "True":
    row["Result"] = "True"
  elif stat1 == "False" and stat2 == "False" or stat1 == "True" and stat2 == "False" or stat1 == "False" and stat2 == "True":
    row["Result"] = "False"
  else:
    row["Result"] = "Other"


# Now we do your group by
df = df.groupby(['OrderID','ID', 'Result'])['Key'].apply(','.join).reset_index()


# Now we populate the columns you wanted populated
df["True"] = ""
df["False"] = ""
df["Other"] = ""
for index, row in df.iterrows():
  if row[row["Result"]]:
    row[row["Result"]] += "," + row["Key"]
  else:
    row[row["Result"]] += row["Key"]
del df['Result']
del df['Key']


# Final we aggregate the rows to flatten it.
df = df.groupby(['OrderID','ID'], as_index=False).agg(lambda x: "%s" % ''.join(x))

相关问题 更多 >