4
   Y2010  Y2011  Y2012  Y2013  test
0  86574  77806  93476  99626  2
1  60954  67873  65135  64418  4
2    156    575    280    330  6
3   1435   1360   1406   1956  7
4   3818   7700   6900   5500  8

Is there a way to rename the columns of this dataframe from Y2010... to 2010.. i.e. removing the initial 'Y'. I want to use regular expressions since I have quite a few such columns. I tried this:

df.rename(df.filter(regex='^Y\d{4}').columns.values, range(2010, 2013 + 1, 1))

--EDIT: The dataframe doees include columns which do not start with a 'Y'

1 Answer 1

13

I'd use map:

In [11]: df.columns.map(lambda x: int(x[1:]))
Out[11]: array([2010, 2011, 2012, 2013])

In [12]: df.columns = df.columns.map(lambda x: int(x[1:]))

In [13]: df
Out[13]:
    2010   2011   2012   2013
0  86574  77806  93476  99626
1  60954  67873  65135  64418
2    156    575    280    330
3   1435   1360   1406   1956
4   3818   7700   6900   5500

Edit: I forgot the most-popular pandas question:

In [21]: df.rename(columns=lambda x: int(x[1:]))
Out[21]:
    2010   2011   2012   2013
0  86574  77806  93476  99626
1  60954  67873  65135  64418
2    156    575    280    330
3   1435   1360   1406   1956
4   3818   7700   6900   5500

If you have additional columns, I would probably write a proper function (rather than a lambda):

def maybe_rename(col_name):
    if re.match(r"^Y\d{4}", col_name):
        return int(col_name[1:])
    else:
        return col_name

In [31]: df.rename(columns=maybe_rename)
Out[31]:
    2010   2011   2012   2013  test
0  86574  77806  93476  99626     2
1  60954  67873  65135  64418     4
2    156    575    280    330     6
3   1435   1360   1406   1956     7
4   3818   7700   6900   5500     8
Sign up to request clarification or add additional context in comments.

2 Comments

my bad, the dataframe does include columns which do not start with a 'Y', which is why I was using filter. Will update question to reflect that
FWIW, maybe_rename worked as written on a MultiIndex, although the only regex matches were in the top level of the Multi-index. I'm not sure it would have behaved otherwise. Cheers!

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.