[英]Create spectrum of rows using columns as boundary pandas data-frame
I have the following pandas
DF:我有以下
pandas
DF:
import pandas as pd
mission_df = pd.DataFrame(
{'mission': [1, 2, 3],
'type': ['lift', 'talk', 'run'],
'boundary_low': [2, 3, 3],
'boundary_high': [3, 8, 12]})
I would like to add rows to each field (example mission) such that each row will be filled according to the boundaries with discrete jumps, for example mission 1 has bounderies between 2 and 3, so i need for that mission to add 2 rows with values 2 & 3, as following:我想向每个字段(示例任务)添加行,以便每一行将根据离散跳跃的边界填充,例如任务 1 的边界在 2 和 3 之间,所以我需要该任务添加 2 行值 2 和 3,如下所示:
desired_df = pd.DataFrame(
{'mission': [1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3],
'amount': [2, 3, 3, 4, 5, 6, 7, 8, 3, 4, 5, 6],
'type': ['lift', 'lift', 'talk', 'talk', 'talk', 'talk', 'talk', 'talk', 'run', 'run', 'run', 'run'],
'boundary_low': [2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
'boundary_high': [3, 3, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6]})
Thanks in advance!提前致谢!
Try:尝试:
mission_df = mission_df.loc[mission_df.index.repeat(mission_df["boundary_high"]-mission_df["boundary_low"] + 1)]
mission_df['amount'] = mission_df.assign(amount=1).groupby(['mission', 'type'])['amount'].cumsum() + mission_df['boundary_low'].sub(1)
# not sure, if relevant for you:
mission_df.reset_index(drop=True, inplace=True)
The key function in here (to make it simple) is:这里的关键 function (为了简单起见)是:
pd.Index.repeat(n)
, src: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.repeat.html pd.Index.repeat(n)
,来源: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.repeat.html
Outputs:输出:
mission type boundary_low boundary_high amount
0 1 lift 2 3 2
1 1 lift 2 3 3
2 2 talk 3 8 3
3 2 talk 3 8 4
4 2 talk 3 8 5
5 2 talk 3 8 6
6 2 talk 3 8 7
7 2 talk 3 8 8
8 3 run 3 12 3
9 3 run 3 12 4
10 3 run 3 12 5
11 3 run 3 12 6
12 3 run 3 12 7
13 3 run 3 12 8
14 3 run 3 12 9
15 3 run 3 12 10
16 3 run 3 12 11
17 3 run 3 12 12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.