Search code examples
pandasdataframedouble-quotescolumnheader

removing double quotes from column header in pandas dataframe


I am trying to understand the intuition behind the following line of code.

I know that it is removing double quotes from the column header within a dataframe, although can anyone please help me understand how it is doing that?

df.columns = [col[1:-1] for col in df.columns]

Thanks


Solution

  • df.columns = ... is the part of line that assigns the list on the right hand side to the columns.

    Then the right hand side is a list comprehension, meaning it can be understood like a for loop.

    Then, in python, a string is an array of characters. for col in columns mean you iterate over each string in the list of columns. Each col is an array. If that string has quotes, then it looks like "xxxx". So the first and the last elements of the array are quotes.

    col[1:-1] is the way to slice the array from the second element to the one before last.

    So when you put all these things next to each other, in your case you end up removing quotes.