如果两个熊猫列值之差大于熊猫中的列值,则将其添加到新行

时间:2020-02-11 11:22:39

标签: python pandas pandas-groupby

''' 这是我的数据样本 '''

PERIOD GROUP USER_COUNT REGION
50    A     55            AX
25    A     20            AX
30    B     33            BY
40    C     10            CZ

预期产量

PERIOD GROUP USER_COUNT REGION
50    A        50         AX
50    A         5         AX
25    A        20         AX
30    B        30         BY
30    B        3          BY
40    C        10         CZ

1 个答案:

答案 0 :(得分:1)

使用:

#get difference of columns
s = df['USER_COUNT'].sub(df['PERIOD']) 
#mask for positive subtract values
m = s > 0

#subtract of original data ony matched rows of column VAL2
df1 = df.assign(USER_COUNT = lambda x: x['USER_COUNT'].sub(s[m], fill_value=0))
#overwrite matched rows
df2 = df[m].assign(USER_COUNT = s[m])

#join together and sorting by only stable sorting - mergesort
df3 = (pd.concat([df1, df2])
         .sort_index(kind='mergesort')
         .reset_index(drop=True)
         .astype(df.dtypes))
print (df3)
   PERIOD GROUP  USER_COUNT REGION
0      50     A          50     AX
1      50     A           5     AX
2      25     A          20     AX
3      30     B          30     BY
4      30     B           3     BY
5      40     C          10     CZ