substring of an entire column in pandas dataframe
In case the column isn't a string, use astype
to convert it:
df['col'] = df['col'].astype(str).str[:9]
Use the str
accessor with square brackets:
df['col'] = df['col'].str[:9]
Or str.slice:
df['col'] = df['col'].str.slice(0, 9)