Home > Mobile >  How to split columns with string after brackets in Pandas
How to split columns with string after brackets in Pandas

Time:11-17

Here is the code:

data = { title:["[US]Tomorrow Never Dies", "[CA]Stories We Tell"]}
df["Country", "Title"] = df['a'].str.split("[ ]", n = 1, expand = True)

I'm trying to make it look like this

Country      Title  
 [US]  Tomorrow Never Dies  
 [CA]    Stories We Tell

CodePudding user response:

We can use str.extract here:

df["Country"] = df["Title"].str.extract(r'\[(.*?)\]')
df["Title"] = df["Title"].str.extract(r'\[.*?\]-?(.*)$')
  • Related