I have dataframe like below
df.head(2)
A B C D E F
1 1 1 3 4 5
3 4 5 2 3 1
I want to dynamically select columns based input passed .
Input
Enter number of columns?
3
Enter column names?
A,E,C
Expected Output
A E C
1 4 1
3 3 5
How can this be done in Python?
iloc:df.iloc[:,:3]for column labels useloc:df.loc[:,['A','E','C']]s="A,B,C", then split like:s.split(',')and pass it under.loc[]as shown above. BDW you should be able to handle cases where user inputs a column which is not there in the dataframe, in such cases usedf.reindex(columns=list_of_cols_after_split)