You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python:基于另一个DataFrame的值重排DataFrame行

问题解决:DataFrame数据重排操作

输入DataFrame

row_df

item    uni_row     count   row
Apple   46             1    [45,46]

tab_df

row     table_no    table_bit   item     space
45          1           0       Lemon     0.25
45          1           1       Melon     0.2
45          1           2       Banana    0.1
45          1           3       Banana    0.1
45          2           1       Apple     0.25
45          2           2       Apple     0.25
46          1           1       Apple     0.25
46          1           2       Orange    0.3

需求说明

根据row_df的数据执行以下操作:

  • 将tab_df中row=46的Apple移动到row=45的位置(即row_df['row']列表排除uni_row后的剩余值)
  • 替换的条目数量要和row_df['count']的值匹配(即替换1条数据)
  • 替换目标需从tab_df中row=45的Apple起始位置倒序选择

预期输出

row     table_no    table_bit   item     space
45          1           0       Lemon     0.25
45          1           1       Apple     0.25      #rearranged
45          1           2       Banana    0.1
45          1           3       Banana    0.1
45          2           1       Apple     0.25
45          2           2       Apple     0.25
46          1           1       Melon     0.2       #rearranged
46          1           2       Orange    0.3

内容的提问来源于stack exchange,提问作者user12345

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 17:15:41