Python 在新列中仅添加“行作为列表”的所有字符串值

Python 在新列中仅添加“行作为列表”的所有字符串值,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据框: TKDM364 3424.32 3244.39 2724.48 1685.24 0 0 TKDM365 3744.64 3458.03 3132.46 2687.91 0 0 TKDM366

我有一个数据框:

TKDM364             3424.32            3244.39            2724.48            1685.24             0            0
TKDM365             3744.64            3458.03            3132.46            2687.91             0            0
TKDM366             3523.18            4007.76            4487.74            2173.04             0            0
TKDM367             3471.77            3888.26            4032.71            4006.34             0            0
TKDM368   LF_Strut_Pressure  RF_Strut_Pressure  LR_Strut_Pressure  RR_Strut_Pressure             4            0
TKDM369   LF_Strut_Pressure  RF_Strut_Pressure  LR_Strut_Pressure  RR_Strut_Pressure             4            0
TKDM374             3361.51            3384.03            2023.38            2263.13             0            0
TKDM378   LF_Strut_Pressure  RF_Strut_Pressure  LR_Strut_Pressure  RR_Strut_Pressure             4            0
TKDM379             4294.54  RF_Strut_Pressure            4399.79            5525.08             1            1
我们在dataframe中看到的奇怪字符串是列标题。这些字符串替换NaN值

我想向数据框中添加一个新列,如果
最后一列中的行值==1,则该列只会为每一行添加列名(字符串格式)

预期输出:
TKDM379应在新添加的列中显示[RF\U支柱\U压力]

换句话说,如果当前
最后一列中的值==1
,则将此行中的所有字符串值添加到列表中,并使此列表成为新列和同一行中的值

PS:列名代替了NaN值(我对python不熟悉,认为如果一行中出现一定数量的NaN值,这将是一种有条件地提取列名的好方法)

以下是一种在每行上使用的方法:

import string
lets = string.ascii_lowercase

df['new_col'] = (df
                 .apply(lambda x: x[x.apply(lambda z: any([y for y in str(z) if y in lets]))] if x[6] == 1 else [], 
                 axis=1)

                  4  5  6              new_col  
0            1685.24  0  0                   []  
1            2687.91  0  0                   []  
2            2173.04  0  0                   []  
3            4006.34  0  0                   []  
4  RR_Strut_Pressure  4  0                   []  
5  RR_Strut_Pressure  4  0                   []  
6            2263.13  0  0                   []  
7  RR_Strut_Pressure  4  0                   []  
8            5525.08  1  1  [RF_Strut_Pressure] 
因为你没有提到else子句,所以我使用的是一个空列表。请根据您的需要随意更改