In the dataframe I have there are so many columns of which I only need a few. For instance
Col_A Col_B Col_C Col_D Col_E Col_F
... ... ... ... ... ... ...
I only need columns Col_A, Col_C and Col_E
so currently what I do is df = df[['Col_A', 'Col_C', 'Col_E']]
but the issue here is that not always there will columns A, C and E
maybe all these wont be present. So I need if Col_A
is in df.columns
add to the df and so on. Is there any simple method to do this ? rather than so many if
? Now if a Column is missing I get a KeyError: "['Col_C'] not in index