Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split column of type interval[int64,right) onto two columns in Pandas

Given a df as follow

  time_interval  dvalue
0        (0, 5]       1
1       (5, 10]       2
2      (10, 15]       4
3      (15, 20]       5
4      (20, 25]       6
5      (25, 30]       7
6      (30, 35]       8

I would like to split the column time_interval, which of type interval[int64, right] as the following

   dvalue   l   u
0       1   0   5
1       2   5  10
2       4  10  15
3       5  15  20
4       6  20  25
5       7  25  30
6       8  30  35

The full code to reproduce the df is as follow

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ls = range(0, 100, 5)
df=pd.DataFrame([1,2,4,5,6,7,8],columns=['dvalue'])
df.index = pd.IntervalIndex.from_breaks(ls[:len(df) + 1])
df.reset_index(inplace=True)
df.rename(columns={'index':'time_interval'},inplace=True)

>Solution :

Use Interval.left and Interval.right:

df['l'] = df['time_interval'].apply(lambda x: x.left)
df['u'] = df['time_interval'].apply(lambda x: x.right)

df['l'] = df['time_interval'].map(lambda x: x.left)
df['u'] = df['time_interval'].map(lambda x: x.right)

Or first convert to IntervalIndex:

idx = pd.IntervalIndex(df['time_interval'])
df['l'] = idx.left
df['u'] = idx.right

idx = pd.IntervalIndex(df['time_interval'])
df = df.assign(l=idx.left, u=idx.right)

print (df)
  time_interval  dvalue   l   u
0        (0, 5]       1   0   5
1       (5, 10]       2   5  10
2      (10, 15]       4  10  15
3      (15, 20]       5  15  20
4      (20, 25]       6  20  25
5      (25, 30]       7  25  30
6      (30, 35]       8  30  35
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading