仅根据下一个NaN行检查项目列表

问题描述 投票:0回答:1

我有以下DataFrame:

            Item  Weight  Bags                 Must  quantity  must quantity  bags str len   assigned bag
0     planes bag    8.50  planes,pods          True     1              1          6                NaN
1  Full Bandolera   3.76  planes               True     1              2          6                NaN
2  tail             0.30  planes               False    3              2          6                NaN
3  central wing     1.08  planes               False    3              2          6                NaN
4  engine           0.44  planes               False    3              2          6                NaN
5  height steer     0.12  planes,pods          False    3              2          6                NaN
6  dihedral         0.40  planes               False    3              2          6                NaN   
7  pods bag         8.72  pods,ground sys...   True     1              1          4                NaN
8  Pod              1.74  pods                 True     3              2          4                NaN
9  optic            0.86  pods                 True     2              2          4                NaN
10 thermal          1.20  ground system bag    True     3              2          4                NaN

如果该袋子在“袋子”列中,我想用袋子的名称填充指定的袋子列。

但是我只想填写下一个空行的分配包,因为在分配每个分配的物品时,我需要检查其他一些条件。

到目前为止,我尝试了以下操作:

BAGS_NAMES = ['planes bag', 'pods bag', 'ground system bag', 'low bag', 'mefaked low']

    @classmethod
    def assign_items_to_bags(cls):
     cls.df[DfConsts.ASSIGNED_BAG] = [bag in BAGS_NAMES for bag in cls.df[DfConsts.BAGS].values.tolist()]
        return cls.df

或此:

    @classmethod
    def assign_items_to_bags(cls):
        cls.df[DfConsts.ASSIGNED_BAG] = cls.df[cls.df[DfConsts.BAGS].isin(BAGS_NAMES)]
        return cls.df

这也是一个问题,因为它不会改变所做的选择。

    @classmethod
    def assign_items_to_bags(cls):
        try:
            selected_bag = random.choice(BAGS_NAMES)
            cls.df[DfConsts.ASSIGNED_BAG] = cls.df.loc[
                                                cls.df[DfConsts.BAGS].str.contains(
                                                    selected_bag)] == selected_bag

我需要这样的东西。

    @classmethod
    def assign_items_to_bags(cls):
        for item in BAGS_NAMES:
            # how to check only the first NaN row?
            if cls.df[DfConsts.BAGS].str.contains(item):
                cls.df[DfConsts.ASSIGNED_BAG] = item
            # try next item in BAGS_NAMES
python pandas dataframe
1个回答
0
投票

我相信您需要:

@classmethod
def assign_items_to_bags(cls):
    for item in BAGS_NAMES:
        # how to check only the first NaN row?
        mask = cls.df[DfConsts.BAGS].str.contains(item)
        cls.df.loc[mask, DfConsts.ASSIGNED_BAG] = item

或:

@classmethod
def assign_items_to_bags(cls):
    for item in BAGS_NAMES:
        # how to check only the first NaN row?
        mask = DfConsts['BAGS'].str.contains(item)
        DfConsts.loc[mask, 'ASSIGNED_BAG'] = item
© www.soinside.com 2019 - 2024. All rights reserved.