Reputation: 1
I have a dataframe column with data like this:
| column_1 |
| -------- |
| 1:3 |
| 1:4 |
| 1:6 |
| 5:6 |
I want to split this column into to two colums like this:
| column_1 | left_split | right_split |
| -------- | -------------- | -------------- |
| 1:3 | 1 | 3 |
| 1:4 | 1 | 4 |
| 1:6 | 1 | 6 |
| 5:6 | 5 | 6 |
Now I have tried using this code:
df['left_split'] = df['column_1'].split(':')[0]
df['right_split'] = df['column_1'].split(':')[1]
and this code:
df['left_split'] = df['column_1'].lsplit(':')
df['right_split'] = df['column_1'].rsplit(':')
and also this:
df['left_split'] = df['column_1'].strip(':')[0]
df['right_split'] = df['column_1'].strip(':')[1]
However, these codes returns an attribute error "'Series' object has no attribute 'split'"
I am confused since when I type this:
df['column_1'].iloc[1].split(':')[0]
it works and displays the left sided number but when I remove the iloc (to apply it on all the column) and assign it to a new column, it does not work.
Would appreciate your help on this
Thanks,
Upvotes: 0
Views: 83
Reputation: 9217
You can use the expand=True
parameter in split, like this:
df[['left_split', 'right_split']] = df['column_1'].str.split(':', expand=True)
Full example:
import pandas as pd
df = pd.DataFrame( {'column_1': {1: '1:3', 2: '1:4', 3: '1:6', 4: '5:6'}})
df[['left_split', 'right_split']] = df['column_1'].str.split(':', expand=True)
print(df)
# column_1 left_split right_split
# 1 1:3 1 3
# 2 1:4 1 4
# 3 1:6 1 6
# 4 5:6 5 6
Upvotes: 1