[英]How to filter columns whose all or some rows values are greater than 0 in Pandas data-frame using Python?
[英]Create spectrum of rows using columns as boundary pandas data-frame
我有以下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]})
我想向每個字段(示例任務)添加行,以便每一行將根據離散跳躍的邊界填充,例如任務 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]})
提前致謝!
嘗試:
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)
這里的關鍵 function (為了簡單起見)是:
pd.Index.repeat(n)
,來源: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.repeat.html
輸出:
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.