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

Time:05-23

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

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)

CodePudding user response:

You can use:

df[['l', 'u']] = [[x.left, x.right] for x in df['time_interval']]

output:

  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

CodePudding user response:

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
  • Related