1

I have the following columns in a pandas df:

Index(['Commodity Derivative Name\n(including associated contracts)',
       'Venue MIC ', 'Name of Trading Venue ', 'Venue Product Codes ',
       'Principal Venue Product Code', 'Spot month single limit#',
       'Other month limit#', 'Conversion Factor', 'Unit of measurement',
       'Definition of spot month', 'Unnamed: 10', 'Unnamed: 11', 'Unnamed: 12',
       'Unnamed: 13', 'Unnamed: 14', 'Unnamed: 15'],
      dtype='object')

I have looked at a few solutions for this, and I am not sure if it is because I am tired, but I cannot get this to work at all.

I guess I could hardcode in the columns but the file could change in the future and thought this would be better to do. I think that maybe after it strips the column in the temp column, it is maybe looking for the unstripped column which is no longer there, so it bugs out - not completely sure.

I have the following code to clean the columns of a df:

f = pd.read_excel(r"fca_position_limits.xlsx")

# unwanted spaces need to be removed from headers
f.columns = f.columns.strip() # --> this did not work

temp_f = f.copy()

for column in f.columns:
    temp_f = temp_f[column].str.strip()
    if column[0:7] == "Unnamed":
        temp_f.drop(column, inplace=True)

1 Answer 1

2

To remove the trailing spaces:

df.columns = [c.strip() for c in df.columns]

and to drop the "Unnamed" columns:

df.drop(columns=df.filter(like='Unnamed').columns)

Here is an example for the drop part:

input:

>>> df = pd.DataFrame([], columns=['A', 'B', 'Unnamed 1', 'Unnamed 2', 'C'])
>>> df.columns
['A', 'B', 'Unnamed 1', 'Unnamed 2', 'C']

output:

>>> df2 = df.drop(columns=df.filter(like='Unnamed').columns)
>>> df2.columns
['A', 'B', 'C']
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, that worked. Would you mind explaining why mine didn't work? Would really help. I also thought that the strip method needs to be assigned to a variable, otherwise the effect goes, or am I wrong on that as well?
Well, df.columns has not strip method. The c.strip() is saved in the list comprehension (and then in the df.columns).
btw, the dropping line of code didn't work for me. I used: for column in f.columns: if column[0:7] == "Unnamed": f.drop(column, inplace=True, axis=1 which ended up working for me.
@Bamir have you saved the output in df? or used df.drop(..., inplace=True)? If this worked with your loop this should work in one shot.
@Bamir I provided an example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.