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

前端 未结 2 1647
盖世英雄少女心
盖世英雄少女心 2020-12-24 06:39

I have a Python dataframe with about 1,500 rows and 15 columns. With one specific column I would like to remove the first 3 characters of each row. As a simple example here

2条回答
  •  执笔经年
    2020-12-24 07:04

    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.2
    
    d = 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.

提交回复
热议问题