python - 将组中的第一个值复制到条件成立的所有行

标签 python pandas

已更新

我有一个 Pandas Dataframe,并且想要在条件满足时使用前行中的值

    df = pd.DataFrame(data=[[1, 2],
                        [1, 4],
                        [1, 2],
                        [1, 3],
                        [1, 2],
                        [5, 3],
                        [1, 4]],
                  columns=['A', 'B'])


df.loc[df.A < df.B, 'B'] =  df.B.shift(1)
df.loc[df.A >= df.B, 'B'] =  df.B

输出:

   A    B
0  1  NaN
1  1  2.0
2  1  4.0
3  1  2.0
4  1  3.0
5  5  3.0
6  1  3.0

但我想要得到的是以下内容:

   A    B
0  1  NaN
1  1  2.0
2  1  2.0
3  1  2.0
4  1  2.0
5  5  3.0
6  1  3.0

那么我如何基本上将 df.B.shift(1) 的结果“写入”数据框中,以便下一行可以再次使用它?

如果满足条件,则取前一行的结果,如果不满足,则保留该值。

最佳答案

我认为您正在寻找一个 groupby 转换 first:

df['B'] = df.groupby((df['A'] >= df['B']).cumsum())['B'].transform('first')
df
   A  B
0  1  2
1  1  2
2  1  2
3  1  2
4  1  2
5  5  3
6  1  3

关于python - 将组中的第一个值复制到条件成立的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53630619/

相关文章:

python - numpy reshape 如何工作?

python - 纯 python 中类似 Django 的单元测试数据库

python - 获取排序的索引列表,用于按给定键排序的字典列表

python - "' 时间戳 ' object does not support indexing", 'occurred at index 0' )

python - 使用 Pandas 数据框中的列作为查找来选择同一 df 中的第二列两次,然后对结果进行比较

python - 性能从 if : pass statements 开始

python - 给定正弦拟合的预测值

python - 在Python中解析APNIC批量whois数据

python - 迭代相同元素的列表

python - 使用 Matplotlib 创建箱线图