3

I want to extact the first 3 from values of a pandas column without doing a loop.

So,

df['myCol'][3]
5475

In order to extract the first 3 digits I do:

int(str(df['myCol'][3])[:2])
547

I want to apply to all the same procedure to the entire column. How can I do it?

Xantium
  • 11,201
  • 10
  • 62
  • 89
emax
  • 6,965
  • 19
  • 74
  • 141
  • 1
    Please read [how to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your post correspondingly. – MaxU - stand with Ukraine May 10 '18 at 15:30

3 Answers3

2

I think need select by indexing with str[] and then cast to integers:

df['myCol'].str[:2].astype(int)

If input values are integers, first cast to strings:

df['myCol'].astype(str).str[:2].astype(int)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

assuming you have a numeric column:

In [189]: df
Out[189]:
    myCol
0    5475
1   99999
2  123456

In [190]: df.dtypes
Out[190]:
myCol    int64
dtype: object

In [191]: df['myCol'] // 10**(np.log10(df.myCol).astype(int) - 2)
Out[191]:
0    547
1    999
2    123
Name: myCol, dtype: int64
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
1

If you like playing with format this one also does the job:

df['myCol'].map(lambda x: '{:.3}'.format(str(x)))
zipa
  • 27,316
  • 6
  • 40
  • 58