substring of an entire column in pandas dataframe

66

I have a pandas dataframe "df". In this dataframe I have multiple columns, one of which I have to substring. Lets say the column name is "col". I can run a "for" loop like below and substring the column:

for i in range(0,len(df)):
  df.iloc[i].col = df.iloc[i].col[:9]

But I wanted to know, if there is an option where I don't have to use a "for" loop, and do it directly using an attribute.I have huge amount of data, and if I do this, the data will take a very long time process.

This question is tagged with python pandas dataframe

~ Asked on 2016-04-08 17:30:17

The Best Answer is


142

Use the str accessor with square brackets:

df['col'] = df['col'].str[:9]

Or str.slice:

df['col'] = df['col'].str.slice(0, 9)

~ Answered on 2016-04-08 17:42:33


0

case the column isn't string, use astype to convert:

df['col'] = df['col'].astype(str).str[:9]

~ Answered on 2021-01-19 13:59:03


Most Viewed Questions: