pandas 如何根据列值创建列表

wixjitnu  于 2023-02-14  发布在  其他
关注(0)|答案(3)|浏览(129)

我有这个 Dataframe :

Text     feat1   feat2   feat3    feat4
string1    1       1       0        0
string2    0       0       0        1
string3    0       0       0        0

我想用这种方法创建另外两列:

Text     feat1   feat2   feat3    feat4     all_feat            count_feat
string1    1       1       0        0       ["feat1","feat2"]       2
string2    0       0       0        1       ["feat4"]               1
string3    0       0       0        0       []                      0

在Python中做这件事的最佳方法是什么?
列名可以是任何字符串。

ijxebb2r

ijxebb2r1#

您可以用途:

df1 = (df.filter(like='feat').mul(df.columns[1:]).apply(lambda x: [i for i in x if i], axis=1)
         .to_frame('all_feat').assign(count=lambda x: x['all_feat'].str.len()))
df = pd.concat([df, df1], axis=1)
print(df)

# Output
      Text  feat1  feat2  feat3  feat4        all_feat  count
0  string1      1      1      0      0  [feat1, feat2]      2
1  string2      0      0      0      1         [feat4]      1
2  string3      0      0      0      0              []      0
q3qa4bjr

q3qa4bjr2#

您可以使用groupby

df2 = df.filter(like='feat').melt(ignore_index=False)
g = df2.groupby(level=0)

df['all_feat'] = g.apply(lambda g: list(g.loc[g['value'].eq(1), 'variable']))
df['count_feat'] = g['value'].sum()

输出:

Text  feat1  feat2  feat3  feat4        all_feat  count_feat
0  string1      1      1      0      0  [feat1, feat2]           2
1  string2      0      0      0      1         [feat4]           1
2  string3      0      0      0      0              []           0
inkz8wg9

inkz8wg93#

col1=df1.set_index("Text").apply(lambda ss:ss.loc[ss>0].index.tolist(),axis=1).reset_index(drop=True)
col2=df1.set_index("Text").apply(lambda ss:ss.loc[ss>0].size,axis=1).reset_index(drop=True)
df1.assign(all_feat=col1,count_feat=col2)

输出

Text  feat1  feat2  feat3  feat4        all_feat  count
0  string1      1      1      0      0  [feat1, feat2]      2
1  string2      0      0      0      1         [feat4]      1
2  string3      0      0      0      0              []      0

相关问题