Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe: truncate string fields

Tags:

pandas

I have a dataframe and would like to truncate each field to up to 20 characters. I've been naively trying the following:

df = df.astype(str).apply(lambda x: x[:20]) 

however it has no effect whatsoever. If, however, I wanted to add an 'Y' to each field, this works like a charm:

df = df.astype(str).apply(lambda x: x+'Y') 

What am I doing wrong?

like image 360
Botond Avatar asked Mar 12 '17 20:03

Botond


2 Answers

Simple one liner to trim long string field in Pandas DataFrame:

df['short_str'] = df['long_str'].str.slice(0,3) 
like image 149
smile-on Avatar answered Oct 17 '22 01:10

smile-on


you can use pandas.Series.str.slice() method:

Demo:

In [177]: df = pd.DataFrame({      ...:   'a': pd.util.testing.rands_array(30, 10),      ...:   'b': pd.util.testing.rands_array(30, 10),      ...: })      ...:  In [178]: df Out[178]:                                 a                               b 0  Mlf6nOsC8S6vv8OxW5ZOWifg3EoqAb  XSGLdkaewwZlNeZ4uTTivi2nMQFc6S 1  0E4XCBaYFBTSalUMPGpXmke6dQGbkW  KlHuVhbNgQL9HLHYQq3fEdqEIciOhX 2  URODJeLA0uLvcKBEXPyrmnnNU40MDl  NaY8LURHjgmT1pRrDnbPAeLZq3ANaL 3  OYA1ahlwVtEVnDOAkZgxNkbvZ7W8Rf  mIzkeLhM7SqYH17vGDzL6DJjSYftGs 4  uFC1shE02UfxS0VhDASmF8vh9XxFYX  fQOxjDjFehTNT27seOtCAAPW0as9Up 5  Ja33vQym6L0Ko2Kcf8cg7OMBKMitg5  iGdCvYTyZlR23NeeTAjG1PoL8mWm3j 6  iNZdXaVpB4zXClxTLt738DY7i6xs6p  q9VKg5fZdItmUpZiQrR6XW5WHmd33l 7  WWnViRRMPkbXNQOHeqGmzETDpGPRl9  t3I8Ve3ybCJcXajF8pydnwNZQWslTN 8  5oMFy2PBe1zUIE3XdraMwlrd5MKcx2  gSLtgXJwiS1HugLORXherFT4l1k5QV 9  weV8BlyJrtRbWpSCxSbj8cSyZxusFR  ylLWort9o8mHWQQ3JB1Twb0xRbLhot  In [179]: df.apply(lambda x: x.str.slice(0, 20)) Out[179]:                       a                     b 0  Mlf6nOsC8S6vv8OxW5ZO  XSGLdkaewwZlNeZ4uTTi 1  0E4XCBaYFBTSalUMPGpX  KlHuVhbNgQL9HLHYQq3f 2  URODJeLA0uLvcKBEXPyr  NaY8LURHjgmT1pRrDnbP 3  OYA1ahlwVtEVnDOAkZgx  mIzkeLhM7SqYH17vGDzL 4  uFC1shE02UfxS0VhDASm  fQOxjDjFehTNT27seOtC 5  Ja33vQym6L0Ko2Kcf8cg  iGdCvYTyZlR23NeeTAjG 6  iNZdXaVpB4zXClxTLt73  q9VKg5fZdItmUpZiQrR6 7  WWnViRRMPkbXNQOHeqGm  t3I8Ve3ybCJcXajF8pyd 8  5oMFy2PBe1zUIE3XdraM  gSLtgXJwiS1HugLORXhe 9  weV8BlyJrtRbWpSCxSbj  ylLWort9o8mHWQQ3JB1T 
like image 22
MaxU - stop WAR against UA Avatar answered Oct 17 '22 01:10

MaxU - stop WAR against UA