Home > Enterprise >  Python, Pandas, drop a column that starts with \n
Python, Pandas, drop a column that starts with \n

Time:07-26

I have the following problem and I do not know how to solve it. I have columns that start with a line break in excel or may for some reason start with \n as provided in the example here.

enter image description here

How do I drop columns that start like that?

df_test = pd.read_excel(r'C:\...\test.xlsx')
df_test.drop("\nSomething")

Simply typing it out like that results in KeyError: "['\\nSomething'] not found in axis".

CodePudding user response:

Try this:

df_test.drop(r"\nSomething", axis=1)

The r stands for raw - it reads '\n' as '\\n'

From the docs:

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters

CodePudding user response:

You could try something like this:

for col in df:
    if col.startswith(r"\n"):
        df.drop(columns=[col], axis=1, inplace=True)

CodePudding user response:

If you want to drop the column \nSomething you can try

df_test.drop("\\nSomething", axis = 1)
  • Related