'How to convert the following R code to Python code

The code drops the columns that are not required from the table:

inputData <- inputData[,!(colnames(inputData) %in% c('col1','col2',''))]

Need help converting this line to python, I'm struggling with it as I'm fairly new to python ;-;



Solution 1:[1]

Assuming you need translation to third-party Pandas which unlike R, data frames are not standard library objects in Python, consider Index.isin to replace %in% and tilde negation operator, ~, to replace !:

inputData = inputData[
    inputData.columns[~inputData.columns.isin(['col1','col2',''])]
]

Alternatively, try Index.difference:

inputData = inputData[
    inputData.columns.difference(['col1','col2',''])
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Parfait