简体   繁体   中英

Pandas dataframe: create new column using conditional and slicing string

I have this piece of code that creates a new dataframe column, using first a conditional, and then slicing some string, with a fixed slicing index (0, 5):

df.loc[df['operation'] == 'dividend', ['order_adj']] = df['comment'].str.slice(0, 5)

But, instead of having a fixed slicing index, I need to use str.find() at the final of this code, to have a dynamic slice index on df['comment'] , based on its characters.

As I'm creating a new column by broadcasting, I couldn't find the correct sintaxe to use str.find('some_string') inside str.slice() . Thanks.

Option using split :

df['comment'].str.split("some_string").str[0]

Or option using regex (move the capture group to be where you want regarding inclusive/exclusive):

pandas.Series.str.extract("(.*?)some_string")
pandas.Series.str.extract("(.*?some_string)")

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