Remove first x number of characters from each row in a column of a Python dataframe Remove first x number of characters from each row in a column of a Python dataframe pandas pandas

Remove first x number of characters from each row in a column of a Python dataframe


Use vectorised str methods to slice each string entry

In [11]:d['Report Number'] = d['Report Number'].str[3:]dOut[11]:     Name Report Number0  George       12345671    Bill       98765432   Sally       4434555


It is worth noting Pandas "vectorised" str methods are no more than Python-level loops.

Assuming clean data, you will often find a list comprehension more efficient:

# Python 3.6.0, Pandas 0.19.2d = pd.concat([d]*10000, ignore_index=True)%timeit d['Report Number'].str[3:]           # 12.1 ms per loop%timeit [i[3:] for i in d['Report Number']]  # 5.78 ms per loop

Note these aren't equivalent, since the list comprehension does not deal with null data and other edge cases. For these situations, you may prefer the Pandas solution.