Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - Edit Index using pattern / regex

Given a data frame like:

>>> df
      ix  val1  val2  val3  val4
    1.31     2     3     4     5
    8.22     2     3     4     5
    5.39     2     3     4     5
    7.34     2     3     4     5

Is it possible to edit index using something like replace?

Pseudo code: (since df index doesnt have str attribute)

df.index=df.index.str.replace("\\.[0-9]*","")

I need something like:

>>> df
   ix  val1  val2  val3  val4
    1     2     3     4     5
    8     2     3     4     5
    5     2     3     4     5
    7     2     3     4     5

The problem is that my dataframe is huge.

Thanks in advance

like image 668
AndreiR Avatar asked Jul 15 '14 00:07

AndreiR


1 Answers

You can do:

df.index = df.index.to_series().astype(str).str.replace(r'\.[0-9]*','').astype(int)

you may also use .extract:

df.index.to_series().astype(str).str.extract(r'(\d+)').astype(int)

alternatively, you may just map the index to int:

pd.Index(map(int, df.index))
like image 155
behzad.nouri Avatar answered Sep 19 '22 19:09

behzad.nouri