Python 使用pandas从两列中获取值范围的优雅方式

Python 使用pandas从两列中获取值范围的优雅方式,python,pandas,dataframe,concatenation,series,Python,Pandas,Dataframe,Concatenation,Series,我有一个如下所示的数据帧(运行下面的完整代码) 请参阅下面我试图获得的范围 df_offset['range_to_shift'] = "[" + (-1 * df_offset['no_days_to_prev_year']).map(str) + "," + df_offset['no_days_to_next_year'].map(str) + "]" 虽然我的方法可行,但我想知道有没有更好、更优雅的方法可以做到这一点 请注意,

我有一个如下所示的数据帧(运行下面的完整代码)

请参阅下面我试图获得的范围

df_offset['range_to_shift'] = "[" + (-1 * df_offset['no_days_to_prev_year']).map(str) + "," + df_offset['no_days_to_next_year'].map(str) + "]"
虽然我的方法可行,但我想知道有没有更好、更优雅的方法可以做到这一点

请注意,对于从
无天数到上一年的值
,我们必须在
前面加上前缀减号

我希望我的输出如下所示


IIUC,您可以使用
zip
创建您的范围列表:

df = pd.DataFrame({'person_id': [11,21,31,41,51],
                   'date_birth': ['05/29/1967', '01/21/1957', '7/27/1959','01/01/1961','12/31/1961']})
df['date_birth'] = pd.to_datetime(df['date_birth'],format="%m/%d/%Y")
df["day_to_prev"] = df['date_birth'].dt.dayofyear - 1
df["day_to_next"] = (pd.offsets.YearEnd(0) + df['date_birth'] - df["date_birth"]).dt.days
df["range_to_shift"] = [[-x, y] for x,y in zip(df["day_to_prev"],df["day_to_next"])]

print (df)

   person_id date_birth  day_to_prev  day_to_next range_to_shift
0         11 1967-05-29          148          216    [-148, 216]
1         21 1957-01-21           20          344     [-20, 344]
2         31 1959-07-27          207          157    [-207, 157]
3         41 1961-01-01            0          364       [0, 364]
4         51 1961-12-31          364            0      [-364, 0]
配合使用:

结果:

# print(df_offset)

   person_id birth_dates  no_days_to_prev_year  no_days_to_next_year range_to_shift
0         11  1967-05-29                   148                   216    [-148, 216]
1         21  1957-01-21                    20                   344     [-20, 344]
2         31  1959-07-27                   207                   157    [-207, 157]
3         41  1961-01-01                     0                   364       [0, 364]
4         51  1961-12-31                   364                     0      [-364, 0]
timeit
性能结果:

df_offset.shape
(50000, 5)

%%timeit -n100
cols = ['no_days_to_prev_year', 'no_days_to_next_year']
df_offset['range_to_shift'] = df_offset[cols].mul([-1, 1]).to_numpy().tolist()

15.5 ms ± 464 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

您想将范围设置为
list
还是
string
range\u-to\u-shift
列中键入?可以提供帮助吗?
# print(df_offset)

   person_id birth_dates  no_days_to_prev_year  no_days_to_next_year range_to_shift
0         11  1967-05-29                   148                   216    [-148, 216]
1         21  1957-01-21                    20                   344     [-20, 344]
2         31  1959-07-27                   207                   157    [-207, 157]
3         41  1961-01-01                     0                   364       [0, 364]
4         51  1961-12-31                   364                     0      [-364, 0]
df_offset.shape
(50000, 5)

%%timeit -n100
cols = ['no_days_to_prev_year', 'no_days_to_next_year']
df_offset['range_to_shift'] = df_offset[cols].mul([-1, 1]).to_numpy().tolist()

15.5 ms ± 464 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)