通过在Python中提取元素来创建新列

2024-09-30 20:23:43 发布

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

我有一个数据框,我想把列表提取到另一列中。你知道吗

df = pd.DataFrame({"Q007_A00":["Y","Y","Y","Y","Y"],
               "Q007_B00": ["N","N","N","N","N"],
               "Q007_C01": [1,4,5,2,"8,3"],
               "Q007_C02": ["Text 1","Text 2","Text 3,Text 4,Text 5","Text 4","Text 5,Text 6"]})

    Q007_A00    Q007_B00    Q007_C01    Q007_C02
 0  Y           N            1          Text 1
 1  Y           N            4          Text 2
 2  Y           N            5          Text 3,Text 4,Text 5
 3  Y           N            2          Text 4
 4  Y           N            8,3        Text 5,Text 6

输出将是

Q007_A00 Q007_B00   Q007_C01 Q007_C01_1 Q007_C02 Q007_C02_1 Q007_C02_2
Y           N           1        0      Text 1    0          0
Y           N           4        0      Text 2    0          0
Y           N           5        0      Text 3    Text 4     Text 5
Y           N           2        0      Text 4    0          0
Y           N           8        3      Text 5    Text 6     0

列名将加1


Tags: 数据textdataframedf列表pd名将c01
1条回答
网友
1楼 · 发布于 2024-09-30 20:23:43

可以将^{}list comprehension^{}一起使用:

df = pd.concat([df[x].astype(str).str.split(',', expand=True) for x in df], 
                axis=1, 
                keys=df.columns).fillna(0)

列中的MultiIndex可以通过list comprehension删除:

df.columns = ['{}_{}'.format(col[0], col[1]) for col in df.columns]
print (df)
  Q007_A00_0 Q007_B00_0 Q007_C01_0 Q007_C01_1 Q007_C02_0 Q007_C02_1 Q007_C02_2
0          Y          N          1          0     Text 1          0          0
1          Y          N          4          0     Text 2          0          0
2          Y          N          5          0     Text 3     Text 4     Text 5
3          Y          N          2          0     Text 4          0          0
4          Y          N          8          3     Text 5     Text 6          0

但是如果需要从列名中删除_0

df.columns = ['{}{}'.format(col[0], '' if col[1] == 0 else '_' + str(col[1])) 
                                                                      for col in df.columns]
print (df)
  Q007_A00 Q007_B00 Q007_C01 Q007_C01_1 Q007_C02 Q007_C02_1 Q007_C02_2
0        Y        N        1          0   Text 1          0          0
1        Y        N        4          0   Text 2          0          0
2        Y        N        5          0   Text 3     Text 4     Text 5
3        Y        N        2          0   Text 4          0          0
4        Y        N        8          3   Text 5     Text 6          0

相关问题 更多 >