页: 1
Below, I have a df with Value
containing various combinations. I want to split the col into two individuals columns, whereby, everything before the last -
and uppercase letters.
页: 1
df = pd.DataFrame({
Value : [ Juan-Diva - HOLLS , Carlos - George - ESTE BAN - BOM , Javier Plain - Hotham Ham - ALPINE , Yul - KONJ KOL MON ],
})
备选办法 1 P-4, 1 P-3, 1 FS, 1 NS
df[[ First , l ]] = df[ Value ].str.split( - , n=1, expand=True)
df[ Last ] = df[ Value ].str.split( - ).str[-1]
备选案文2
# Regular expression pattern
pattern = r ^(.*) - ([A-Zs]+)$
# Extract groups into two new columns
df[[ First , Last ]] = df[ Value ].str.extract(pattern)
option 3)
df[["First", "Last"]] = df["Value"].str.rsplit(" - ", n=1, expand=True)
None of these options return the intended output.
预期产出:
First Last
0 Juan-Diva HOLLS
1 Carlos - George ESTE BAN - BOM
2 Javier Plain - Hotham Ham ALPINE
3 Yul KONJ KOL MON