Write a program to select all columns of a dataframe except the ones specified.

Write a program to select all columns of a dataframe except the ones specified. the input will contain a list of columns that you should skip. you should print the first five rows of the dataframe as output where the columns are alphabetically sorted

Click here to download csv

Sample Input: ['PassengerId', 'Pclass', 'Name', 'Sex','Embarked']

Sample Output:

    Age Cabin     Fare  Parch  SibSp   Ticket
0  34.5   NaN   7.8292      0      0   330911
1  47.0   NaN   7.0000      0      1   363272
2  62.0   NaN   9.6875      0      0   240276
3  27.0   NaN   8.6625      0      0   315154
4  22.0   NaN  12.2875      1      1  3101298

Pandas select all columns except

import pandas as pd
import ast,sys

df=pd.read_csv("test.csv")
input_str = sys.stdin.read()
to_omit = ast.literal_eval(input_str)

df=df[df.columns[~df.columns.isin(to_omit)]]
print(df.loc[:, sorted(list(df.columns))].head())

Comments