Reputation: 4055
I have a dataframe column which I want to split into 3 separate columns.
o_order_df
expiry
0 27-Sep-2018-260-CE
1 27-Sep-2018-250-CE
o_order_df[['expiry','strike','type']] = o_order_df['expiry'].str.split('-', n>=2, expand=True)
I want to split
the column
on the third occurrence of -
.
I keep getting an error:
Traceback (most recent call last): File "", line 1, in NameError: name 'n' is not defined
Expected output
o_order_df
expiry strike type
0 27-Sep-2018 260 CE
1 27-Sep-2018 250 CE
I am able to split
the column by delimiter.
Upvotes: 2
Views: 1313
Reputation: 862511
Use n=2
with rsplit
:
o_order_df[['expiry','strike','type']] = o_order_df['expiry'].str.rsplit('-',n=2,expand=True)
If performance is important use list comprehension with rsplit
:
c = ['expiry','strike','type']
df = pd.DataFrame([x.rsplit('-', 2) for x in o_order_df['expiry']], columns=c)
print (df)
expiry strike type
0 27-Sep-2018 260 CE
1 27-Sep-2018 250 CE
Upvotes: 2