Pooja Gangurde
Pooja Gangurde

Reputation: 103

Splitting String in two parts in Python

I have a column with the values like these

EUR, London , Germany
USD , New York , Boston, Canada
INR , Delhi, Mumbai

I am trying to extract the portion after the first ',' into another column, something like this

Desired output:
    London , Germany
    New York , Boston, Canada
    Delhi, Mumbai

but, when I use Events['Col2'] = Events['OriginalColumn'].str.split(',').str[1:7], I get the desired however its in array, I dont the result to be come in square brackets and in quotes. Below is what I get

[' London','Germany']
[' New York','Boston','Canada']
['Delhi', 'Mumbai']

Is there a way in which we can avoid the square brackets and the quotes. thanks in advance.

Upvotes: 0

Views: 118

Answers (2)

Sameeresque
Sameeresque

Reputation: 2602

Assuming x is what you started your question with:

for i in x:
    row=i.split(',')[1:]
    print(' '.join([(row[k]) for k in range(len(row))]))

Upvotes: 0

Heike
Heike

Reputation: 24440

I'm assuming the column you are referring to is a column in a pandas DataFrame. In that case you can use the parameter n of pandas.Series.str.split to limit the number of splits to one, e.g.

Events['Col2'] = Events['OriginalColumn'].str.split(',', n=1).str[1]

Upvotes: 1

Related Questions