Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming columns in pandas dataframe using regular expressions

Tags:

python

pandas

   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'

like image 729
user308827 Avatar asked Dec 19 '22 23:12

user308827


1 Answers

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
like image 138
Andy Hayden Avatar answered Apr 05 '23 23:04

Andy Hayden