Pandas - Edit index using pattern / regular expression

Given a data frame, for example:

>>> 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 an index using something like replace ?

Pseudo-code: (since the df index has no 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

+4
source share
1 answer

You can do:

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

you can also use .extract:

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

you can just mapspecify the index int:

pd.Index(map(int, df.index))
+5
source

Source: https://habr.com/ru/post/1548420/


All Articles