so i got a numeric list [0-12] that matches the length of my columns in my spreadsheet and also replaced the column headers with that list >> df.columns = list Now i want to drop specific columns out of that spreadsheet like this: enter image description here
To create the list of numbers to match the length of columns i got this:
listOfNumbers = []
column_name = []
for i in range(0, len(df.columns)):
listOfNumbers.append(i)
df.columns = listOfNumbers
for i in range(1, len(df.columns)):
for j in range(1, len(df.columns)):
if i != colList[j]:
df.drop(i, inplace=True)
and i got the list [1,2,3] as seen in the picture. But i always get this Error: KeyError: '[1] not found in axis'
i tried to replace df.drop(i, inplace=True) with df.drop(i, axis=1, inplace=True) but that didnt work either
Any suggestions? Thanks.
CodePudding user response:
the proper way will be:
columns_to_remove = [1, 2, 3] # columns to delete
df = df.drop(columns=df.columns[columns_to_remove])
So for your use case:
for i in range(1, len(df.columns)):
for j in range(1, len(df.columns)):
if i != colList[j]:
df.drop(columns=df.columns[i], inplace=True)
CodePudding user response:
If you want to drop every column that does not appear in colList
, this code does it, using set difference:
setOfNumbers = set(range(df.shape[1]))
setRemainColumns = set(colList)
for dropColumn in setOfNumbers.difference(setRemainColumns):
df.drop(dropColumn, axis=1, inplace=True)