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
  • 10,258
  • 9
  • 56
  • 83
emax
  • 5,795
  • 11
  • 55
  • 115
  • 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 - stop genocide of UA 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
  • 729,927
  • 78
  • 1,141
  • 1,090
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 - stop genocide of UA
  • 191,778
  • 30
  • 340
  • 375
1

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

df['myCol'].map(lambda x: '{:.3}'.format(str(x)))
zipa
  • 26,044
  • 6
  • 38
  • 55