简体   繁体   中英

How can I use a single quote to split one column into two within a pandas dataframe?

I have a column ( samplename_date ) of a dataframe that looks as follows:

'008Q06-03 RGD17-48 3.8W Wm 1xtl' 03 July

I'm trying to split this into two columns, after the second single quote. I've been trying:

temp[['samplename','date']] = df['samplename_date'].str.split(''\\s', expand = True)

and variations thereof, but I can't seem to figure out how to handle the single quote in the regular expression pattern.

You need to escape single quote as well

temp[['samplename','date']] = df['samplename_date'].str.split('\'\s', expand = True)

You get

    samplename                          date
0   '008Q06-03 RGD17-48 3.8W Wm 1xtl    03 July

I would personally use str.extract

temp[['samplename','date']] = df['samplename_date'].str.extract('\'(.*)\'\s(.*)', expand = True)


    samplename                      date
0   008Q06-03 RGD17-48 3.8W Wm 1xtl 03 July

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