从另一列的子字符串创建列

时间:2018-04-13 01:23:40

标签: python python-3.x pandas dataframe

我有一个pandas数据框对象。我想从现有列的子字符串创建新列。我的数据如下:

    Date        variable                 want1          want2   want3
0   02-01-08    Australia - Sydney - A   Australia      Sydney  A
1   03-01-08    Australia - Sydney - A   Australia      Sydney  A
2   04-01-08    Australia - Sydney - A   Australia      Sydney  A
3   05-01-08    Canada - Toronto - B     Canada         Toronto B
4   06-01-08    Canada - Toronto - B     Canada         Toronto B

其中want1 to want3是我需要的。

2 个答案:

答案 0 :(得分:3)

您可以使用pd.Series.str.split

df[['want1', 'want2', 'want3']] = df['variable'].str.split(' - ', expand=True)

答案 1 :(得分:1)

set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\ameet\AppData\Local\go-build set GOEXE=.exe set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOOS=windows set GOPATH=F:\OneDrive\Projects\gows set GORACE= set GOROOT=C:\Go set GOTMPDIR= set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64 set GCCGO=gccgo set CC=gcc set CXX=g++ set CGO_ENABLED=1 set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ameet\AppData\Local\Temp\go- build874557962=/tmp/go-build -gno-record-gcc-switches

F:\OneDrive\Projects\gows\src\hello>cat hello.go
    package main

    import "fmt"

    func main() {
        fmt.Printf("hello, world\n")
    }
F:\OneDrive\Projects\gows\src\hello>go build hello.go
can't load package: package main: cannot find package "." in:
        F:\OneDrive\Projects\gows\src\hello