I have col_list = [ "A","B","C","D","E"]
My dataframe has column [ "A","B","X","Y","Z"]
If I run below code I will obviously get error "not in index" as C,D,E is not there BUT I want code to ignore that and add if any column matches with column in list (here it should add A B)
df = df[df[col_list].sum(axis=1) == 0]
I used intersection but it dropped X,Y,Z from dataframe so can't use that.
CodePudding user response:
You can put your code inside a try block:
try:
df = df[df[col_list.sum(axis=1) == 0]
except IndexError:
pass # or you can do whatever you want
CodePudding user response:
Use reindex
instead:
df.reindex(columns=col_list, fill_value=0).sum(axis=1)
Or you intersect
:
df[df.columns.intersect(col_list)].sum(axis=1)
CodePudding user response:
I am presuming you want to add a new column containing the sum of specific columns.
import pandas as pd
df = pd.DataFrame([(1,2,3,4,5,)],columns=[ "A","B","X","Y","Z"])
col_list = [ "A","B","C","D","E"]
df["SUM"] = df[df.columns.intersection(col_list)].sum(axis=1)
The DataFrame will keep all columns and it will have a new one. No columns dropped.