Reputation: 3919
I have a standard Series in Python and I want to split each element based upon a delimiter.
What I am currently using is
for i in Series:
i.split(' ')
This however returns only the last element in the Series.
I want to be able to kind of 'capture' each thing that is split as the loop works through the Series and then write them back to that (or another) Series.
I hope that makes sense :-S
Upvotes: 2
Views: 4398
Reputation: 1017
One way is to split and keep it in the same series object:
In [3]: s = pd.Series(['Element1 , Element2'])
In [7]: s
Out[7]:
0 Element1 , Element2
dtype: object
In [8]: s.str.split(',')
Out[8]:
0 [Element1 , Element2]
dtype: object
If you like to split a Series into two columns or basically two series you could use expand=True
parameter:
In [12]: s.str.split(',', expand=True)
Out[12]:
0 1
0 Element1 Element2
In [3]: type(s.str.split(',', expand=True))
Out[3]: pandas.core.frame.DataFrame
Upvotes: 2
Reputation: 863751
I think you need str.split
- for splitting by whitespace(s) is possible omit pat
parameter, because if None
, splits on whitespace. Output are lists.
s1 = s.str.split()
s = pd.Series(['s d','f g','d'])
print (s)
0 s d
1 f g
2 d
dtype: object
s1 = s.str.split()
print (s1)
0 [s, d]
1 [f, g]
2 [d]
dtype: object
Upvotes: 3