简体   繁体   中英

Split column and only keep one part in Pandas

I'm splitting a column, and I'm only keeping the second part. It seems inefficient to do the spit operation, and then drop the first column. Is there a way to just keep the new column?

   df[["Start","StartTime"]] = df.StartTime.str.rsplit("I",n=-1, expand=True)
    df = df.drop('Start', 1)

You can use a regular expression with .str.extract :

so that you only keep the group you are interested in (adapt the pattern to your needs):

# This pattern will group everything after the first 'I' up to the end ('$').
df.StartTime.str.extract(r'I(.*)$', expand=True)

However I doubt using a regex instead of a split operation will be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM