简体   繁体   中英

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.

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, eg

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

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))]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM