0

I want to remove some characters from 'name' columns, in a way to keep the very first characters and remove the rest

So this is my data:

   name   id
0  ABC-G  3
1  ERT-R  4
2  IGF    2

The result should be:

   name   id
0  AB     3
1  ER     4
2  IG     5
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555

1 Answers1

-1

You can str.slice(..) [pandas-doc] the column, like:

df['name'] = df['name'].str.slice(stop=2)

Or if you use some sort of filtering:

df.loc[some_filter, 'name'] = df[some_filter]['name'].str.slice(stop=2)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • ":\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead" this is the message i get when i tap this command – zahir safaa Aug 14 '19 at 20:15
  • @zahirsafaa: yes, that is a common warning. Did you by any chance used a filter on this line? See https://stackoverflow.com/q/20625582/67579 – Willem Van Onsem Aug 14 '19 at 20:20