I would like to know the appropriate operation with the list of columns ['a','b','d'] and df as inputs.推荐答案You can just sum and set param axis=1 to sum the rows, this will ignore none数字列:You can just sum and set param axis=1 to sum the rows, this will ignore none numeric columns:In [91]:df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['dd','ee','ff'], 'd':[5,9,1]})df['e'] = df.sum(axis=1)dfOut[91]: a b c d e0 1 2 dd 5 81 2 3 ee 9 142 3 4 ff 1 8如果您只想对特定列求和,则可以创建列列表并删除您不感兴趣的列:If you want to just sum specific columns then you can create a list of the columns and remove the ones you are not interested in:In [98]:col_list= list(df)col_list.remove('d')col_listOut[98]:['a', 'b', 'c']In [99]:df['e'] = df[col_list].sum(axis=1)dfOut[99]: a b c d e0 1 2 dd 5 31 2 3 ee 9 52 3 4 ff 1 7 这篇关于Pandas:对给定列的 DataFrame 行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 23:17