簡體   English   中英

根據值將列表的Pandas列拆分為多列

[英]Split Pandas column of lists into multiple columns based on value

我有一個 Pandas Dataframe 有一個列表列。 我想根據值將此列表列拆分為多個列。 根據列名為每條記錄返回 yes_value 或 no_value。

輸入示例:

id | values
---|----------
1  | [A,B,C,D]
2  | [D,E,F]
3  | [A,D]
4  | [K]

預計 output:

id | values   |  A    |   B   |   C   |   D   |   E   |   F   |    K  |
---|----------|-------|-------|-------|-------|-------|-------|-------|
1  | [A,B,C,D]| yes_A | yes_B | yes_C | yes_D |  no_E |  no_F |  no_K |
2  | [D,E,F]  | no_A  | no_B  | no_C  | yes_D | yes_E | yes_F |  no_K |
3  | [A,D]    | yes_A | no_B  | no_C  | yes_D |  no_E |  no_F |  no_K | 
4  | [K]      | no_A  | no_B  | no_C  |  no_D |  no_E |  no_F | yes_K | 

您可以使用crosstab來重塑:

df2 = df.explode('values')
df3 = pd.crosstab(df2['id'], df2['values']).replace({0: 'no_', 1: 'yes_'})

out = df.merge(df3.add(df3.columns), left_on='id', right_index=True)

或者str.get_dummies

df2 = df['values'].agg('|'.join).str.get_dummies().replace({0: 'no_', 1: 'yes_'})
out = df.join(df2.add(df2.columns))

output:

   id        values      A      B      C      D      E      F      K
0   1  [A, B, C, D]  yes_A  yes_B  yes_C  yes_D   no_E   no_F   no_K
1   2     [D, E, F]   no_A   no_B   no_C  yes_D  yes_E  yes_F   no_K
2   3        [A, D]  yes_A   no_B   no_C  yes_D   no_E   no_F   no_K
3   4           [K]   no_A   no_B   no_C   no_D   no_E   no_F  yes_K

代碼:

#Input
df = pd.DataFrame({'id':[1,2,3,4], 'Values':[['A','B','C','D'], ['D','E','F'], ['A','D'],  ['K']]})

#STEP 1 merging the Values column all lists into one and findout the unique values using set
#so the output will be {'A', 'B', 'C', 'D', 'E', 'F', 'K'} and then looping on it as below
for i in sorted(set(sum(df['Values'].tolist(),[]))):  

    #STEP 2 Creating new column and check if column in list or not
    df[i] = df['Values'].apply(lambda x: f'yes_{i}' if i in x else f'no_{i}')
df

Output:

    id  Values          A       B       C       D       E       F    K
0   1   [A, B, C, D]    yes_A   yes_B   yes_C   yes_D   no_E    no_F    no_K
1   2   [D, E, F]       no_A    no_B    no_C    yes_D   yes_E   yes_F   no_K
2   3   [A, D]          yes_A   no_B    no_C    yes_D   no_E    no_F    no_K
3   4   [K]             no_A    no_B    no_C    no_D    no_E    no_F    yes_K

暫無
暫無

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

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