我有一个具有以下模式的列的df数据框:#number - letter,我想添加一个新的列other,它将不在letter_table1和letter_table2中的列相加
TEXT, A, B, C, D, E, F, G, H, I
a,1,1,1,2,2,2,3,3,3
b,1,1,1,2,2,2,3,3,3
c,1,1,1,2,2,2,3,3,3
d,1,1,1,2,2,2,3,3,3
e,1,1,1,2,2,2,3,3,3
f,1,1,1,2,2,2,3,3,3
g,1,1,1,2,2,2,3,3,3
h,1,1,1,2,2,2,3,3,3
i,1,1,1,2,2,2,3,3,3
j,1,1,1,2,2,2,3,3,3例如:
tableau_lettres1 = [H]
tableau_lettres2 = [I, J]我该怎么做呢?目前我已经尝试过了:
df_sum['others'] = df.loc[:,~df.isin(tableau_lettres1, tableau_lettres2)].sum(axis=1)以及:
df_sum['others'] = df.loc[:,df.drop(tableau_lettres1, tableau_lettres2)].sum(axis=1)发布于 2019-10-15 16:30:34
由于tableau_lettres1, tableau_lettres2是列表,您需要将它们连接到一个列表中,并获取其他列名,如下所示:
df_sum['others'] = df[[col for col in df.columns.tolist() if col not in tableau_lettres1 + tableau_lettres2]].sum(axis=1)https://stackoverflow.com/questions/58390232
复制相似问题