如果不匹配,则数据框分配一揽子标准

时间:2019-01-11 17:00:43

标签: python pandas dataframe

我有一个按照以下方式组织的用于铁路车辆的数据框。 我想按['Railroad']进行计数,但前提是它与“ VER”匹配。如果没有,我希望“铁路”将值重新分配为“不”并以此计数。

数据框层次结构:

df1 = df.reset_index().groupby(['Homebase','FINAL ETA','Code Description','L_E', 'Railroad'])['Code Description'].size()

示例输出:

Homebase    FINAL ETA   Code Description        L_E  Railroad   
Rail2       2018-12-06  Arrival in yard         L    VER          1
            2019-01-04  Arrival in yard         L    VER         10
            2019-01-08  Arrival in yard         L    FIL         16
            2019-01-09  Arrival in yard         L    FIL          5
            2019-01-13  Arrival in yard         L    PAS          1

所需的输出:

Homebase    FINAL ETA   Code Description        L_E  Railroad
Rail2       2018-12-06  Arrival in yard         L    VER          1
            2019-01-04  Arrival in yard         L    VER         10
            2019-01-08  Arrival in yard         L    NOT         16
            2019-01-09  Arrival in yard         L    NOT          5
            2019-01-13  Arrival in yard         L    NOT          1

1 个答案:

答案 0 :(得分:0)

似乎只有铁路栏目正在改变,请尝试以下操作:

ver = (df1['Railroad'] == 'VER')
df1['Railroad'] = 'NOT'
df1.loc[ver, 'Railroad'] = 'VER'