简体   繁体   中英

Extract on “-” delimited in pandas

I have a pandas series with some values like 19.99-20.99 (ie two numbers separated by a dash). How would you just take the left or right value?

Use split("-") on the resulting string and then access the result with index notation, ie split_result[1] .

Here's an example:

In [5]: my_series = pandas.Series(['19.22-20.11','18.55-34.22','12.33-22.00','13.33-34.23'])

In [6]: my_series[0]
Out[6]: '19.22-20.11'

In [7]: my_series[0].split("-")
Out[7]: ['19.22', '20.11']

In [8]: my_series[0].split("-")[0]
Out[8]: '19.22'

In [9]: my_series[0].split("-")[1]
Out[9]: '20.11'
In [1]: s = pd.Series(['19.99-20.99', '20.99-21.99'])

In [2]: s.str.split('-').str[0]
Out[2]: 
0    19.99
1    20.99
dtype: object

In [3]: s.str.split('-').str[1]
Out[3]: 
0    20.99
1    21.99
dtype: object

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