我有以下数据框:

作为PD进口大Pandas

data = [['Construction', '', '01/02/2022', '01/06/2022', '1', 'No'], ['Level Site', 'Construction', '01/02/2022', '01/02/2022', '2', 'No'], ['Foundation', '', '01/03/2023', '01/06/2023', '1', 'Yes'],['Lay Foundation', 'Construction>Foundation', '01/03/2022', '01/04/2022', '3', 'No'], ['Prepare land for foundation', 'Construction>Foundation', '01/05/2022', '01/06/2022', '3', 'No'],['Building Envelope', '', '01/07/2023', '01/16/2023', '1', 'No'], ['Install Footings', 'Building Envelope', '01/07/2022', '01/07/2022', '2', 'Yes'], ['Pouring', '', '01/08/202', '01/09/2023', '1', 'No'],['Pour Foundation', 'Building Envelope>Pouring', '01/08/2022', '01/09/2022', '3', 'No'], ['Installation', '', '01/09/2022', '01/14/2022', '1', 'No']]
df1 = pd.DataFrame(data, columns=['Activity', 'Parent', 'Start', 'Finish', 'WBS Level', 'Match'])

df1

#理想数据帧输出

data = [['Construction', '', '01/02/2022', '01/06/2022', '1', 'No'],['Foundation', '', '01/03/2023', '01/06/2023', '1', 'Yes'], ['Level Site', 'Construction', '01/02/2022', '01/02/2022', '2', 'No'], ['Lay Foundation', 'Construction>Foundation', '01/03/2022', '01/04/2022', '3', 'No'], ['Prepare land for foundation', 'Construction>Foundation', '01/05/2022', '01/06/2022', '3', 'No'],['Install Footings', 'Building Envelope', '01/07/2022', '01/07/2022', '2', 'Yes'],['Building Envelope', '', '01/07/2023', '01/16/2023', '1', 'No'], ['Pouring', '', '01/08/202', '01/09/2023', '1', 'No'],['Pour Foundation', 'Building Envelope>Pouring', '01/08/2022', '01/09/2022', '3', 'No'], ['Installation', '', '01/09/2022', '01/14/2022', '1', 'No']]
df2 = pd.DataFrame(data, columns=['Activity', 'Parent', 'Start', 'Finish', 'WBS Level', 'Match'])

df2

我正在准备在调度软件应用程序中使用这些数据,需要根据某些条件重新排序行.为此,我创建了‘Match’列(我已经创建了我的条件,任何‘yes’行都满足该条件).

对于"Match"列中有"yes"值的任何行,我要上移一行.我已经try 了.Shift方法的各种变体,但在正确使用时遇到了问题.我不想删除或覆盖任何行,我只需要将任何"是"行上移1.

谢谢你的帮助

推荐答案

这里有一个解决方案,它使用索引而不是使用.shift()来交换所需的行(因为我不清楚在groupby()中如何做到这一点).可能不会很好地扩展,但在较小的数据集上应该可以做到这一点.

df1 = df1.reset_index(drop=True)  # ensure index is unique

# Loop through only the indices of rows to be shifted, to avoid looping through every row
shift_indices = df1[df1['Match'] == 'Yes'].index
for shift_idx in shift_indices:
    # No need to shift if at the top
    if shift_idx == 0:
        continue
    above_idx = shift_idx - 1
    above_row = df1.loc[above_idx].copy()  # copy as otherwise this row will change during the shift
    # If the row above is also a match, then no need to swap it
    if above_row['Match'] != 'Yes':
        shift_row = df1.loc[shift_idx]
        df1.loc[above_idx] = shift_row
        df1.loc[shift_idx] = above_row

Python相关问答推荐

按照行主要蛇扫描顺序对点列表进行排序

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

在matplotlib动画gif中更改配色方案

如何使用Google Gemini API为单个提示生成多个响应?

运行回文查找器代码时发生错误:[类型错误:builtin_index_or_system对象不可订阅]

SQLGory-file包FilField不允许提供自定义文件名,自动将文件保存为未命名

查找两极rame中组之间的所有差异

计算组中唯一值的数量

Python逻辑操作作为Pandas中的条件

在pandas数据框中计算相对体积比指标,并添加指标值作为新列

如何在BeautifulSoup/CSS Select 器中处理regex?

以逻辑方式获取自己的pyproject.toml依赖项

在matplotlib中使用不同大小的标记顶部添加批注

BeautifulSoup-Screper有时运行得很好,很健壮--但有时它失败了::可能这里需要一些更多的异常处理?

查看pandas字符列是否在字符串列中

如何在Gekko中处理跨矢量优化

简单 torch 模型测试:ModuleNotFoundError:没有名为';Ultralytics.yolo';

用0填充没有覆盖范围的垃圾箱

时长超过24小时如何从Excel导入时长数据

如何在python tkinter中绑定键盘上的另一个回车?