我有一個資料框,其中有一列包含某些值,我想在其中提取連字符后的第二個字串并保留原??始值。
資料
type stat
SSS-AA-11111 y
FFF-BB-22222 y
期望的
type type1 stat
SSS-AA-11111 AA y
FFF-BB-22222 BB y
正在做
df[['type', 'type1']] = df['type'].str.split('-', 1, expand=True)`
我收到此錯誤:
ValueError: Columns must be same length as key
我還在研究。任何建議表示贊賞
uj5u.com熱心網友回復:
使用str.split并保留第二個元素str[1]:
df['type1'] = df['type'].str.split('-').str[1]
print(df)
# Output
type stat type1
0 SSS-AA-11111 y AA
1 FFF-BB-22222 y BB
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410465.html
標籤:
