仅将 pandas 行的所有字符串值添加为新列中的列表 [英] Add only all string values of pandas row as list in a new column
问题描述
我有一个熊猫数据框:
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
在数据框中看到的奇怪字符串是列标题。这些字符串替换NaN值
The strange strings that we see in the dataframe, are the column headers. These strings replace NaN values
我想向数据框中添加新列,如果该行中的行值仅会为每一行添加列名(以字符串格式) 最后一列== 1
。
I want to add a new column to the dataframe that will only add the column names (in string format) for each row IF the row value in the last column == 1
.
预期输出: TKDM379应该在新添加的列中显示[RF_Strut_Pressure]
。
换句话说,如果当前最后一列中的值== 1
,则将所有字符串值相加在此行中找到一个列表,并将此列表作为新列中同一行的值
In other words, IF the value in the current last column == 1
, THEN add all the string values within this row to a list, and let this list be the value in a new column and same row
PS:将列名替换为NaN值(是python的新手,并认为如果连续出现一定数量的NaN值,这将是有条件提取列名的好方法)
PS: The column names were put in place of NaN values (I am new to python and thought this would be a good way of conditionally extract column names if a certain amount of NaN values appear in a row)
推荐答案
这是使用每行 .apply
:
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子句,因此我使用的是空列表。随时根据需要进行更改。
Since you didn't mention the else clause, I am using an empty list. Feel free to change it according to your need.
这篇关于仅将 pandas 行的所有字符串值添加为新列中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!