7

I have a dataframe like this:

                Basic Stats        Min       Max      Mean     Stdev   
1        LT50300282010256PAC01   0.336438  0.743478  0.592622  0.052544   
2        LT50300282009269PAC01   0.313259  0.678561  0.525667  0.048047   
3        LT50300282008253PAC01   0.374522  0.746828  0.583513  0.055989   
4        LT50300282007237PAC01  -0.000000  0.749325  0.330068  0.314351   
5        LT50300282006205PAC01  -0.000000  0.819288  0.600136  0.170060 

and for the column Basic Stats I want to retain only the characters between [9:12] so for row 1 I only want to retain 2010 and for row 2 I only want to retain 2009. Is there a way to do this?

Stefano Potter
  • 3,467
  • 10
  • 45
  • 82

4 Answers4

12

Just use vectorised str method to slice your strings:

In [23]:

df['Basic Stats'].str[9:13]
Out[23]:
0    2010
1    2009
2    2008
3    2007
4    2006
Name: Basic Stats, dtype: object
EdChum
  • 376,765
  • 198
  • 813
  • 562
4

One way would be to use

df['Basic Stats'] = df['Basic Stats'].map(lambda x: x[9:13])
Joseph Stover
  • 397
  • 4
  • 13
1

You can slice

df["Basic Stats"] = df["Basic Stats"].str.slice(9,13)

Output:

  Basic Stats       Min       Max      Mean     Stdev
0        2010  0.336438  0.743478  0.592622  0.052544
1        2009  0.313259  0.678561  0.525667  0.048047
2        2008  0.374522  0.746828  0.583513  0.055989
3        2007 -0.000000  0.749325  0.330068  0.314351
4        2006 -0.000000  0.819288  0.600136  0.170060
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

You can do:

df["Basic Stats"] = [ x[9:13] for x in df["Basic Stats"] ]
YOBA
  • 2,759
  • 1
  • 14
  • 29