相关疑难解决方法(0)

确定列的累积最大值

我正在尝试以下代码

df = pd.DataFrame([[23, 52], [36, 49], [52, 61], [75, 82], [97, 12]], columns=['A', 'B'])
df['C'] = np.where(df['A'] > df['C'].shift(), df['A'], df['C'].shift())
print(df)
Run Code Online (Sandbox Code Playgroud)

假设第一个df['C].shift()操作应假设为0(因为df['C']不存在)

预期产出

    A   B   C
0  23  52  23
1  36  49  36
2  12  61  36
3  75  82  75
4  70  12  75
Run Code Online (Sandbox Code Playgroud)

但我得到一个KeyError异常.

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

0
推荐指数
1
解决办法
266
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1