Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对不在列表中的列进行分组和求和_Python_Python 3.x_Dataframe_Sum - Fatal编程技术网

Python 对不在列表中的列进行分组和求和

Python 对不在列表中的列进行分组和求和,python,python-3.x,dataframe,sum,Python,Python 3.x,Dataframe,Sum,我有一个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

我有一个
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)

由于
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)

请共享您的数据帧数据和输入输出?