繁体   English   中英

根据关联值将值连接到多列中

[英]Concatenate values into multiple columns based on associated value

给定一个数据框

+----+-------+------+-----------+-----------+---------------+
|    |   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都为 True,则连接的Keys应位于TRUE列中。 如果其中任何一个是Flase ,则Keys应位于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

这是一个可行的解决方案,但绝对有一种更快、更干净的方法来做到这一点。 首先,我为您的布尔逻辑添加一列,然后我执行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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM