Pandas: Split a DataFrame column

19 August 2020

Code

# Import library
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'label': ['super', 'awesome', 'fantastic'],
    'id': ['1000982-OEKSI', '1000983-OEKSI', '1001001-OEKSI']
})

print(df)
       label             id
0      super  1000982-OEKSI
1    awesome  1000983-OEKSI
2  fantastic  1001001-OEKSI

.

# Split column and expand
df[['id_num', 'id_txt']] = df['id'].str.split('-', expand=True)

# Output
print(df)

Output:

       label             id   id_num id_txt
0      super  1000982-OEKSI  1000982  OEKSI
1    awesome  1000983-OEKSI  1000983  OEKSI
2  fantastic  1001001-OEKSI  1001001  OEKSI






Any errors in code above?
Please send a message.