Обратная накопительная сумма столбца в pandas.DataFramePython

Программы на Python
Anonymous
Обратная накопительная сумма столбца в pandas.DataFrame

Сообщение Anonymous »


I've got a pandas DataFrame with a boolean column sorted by another column and need to calculate reverse cumulative sum of the boolean column, that is, amount of true values from current row to bottom.

Example

In [13]: df = pd.DataFrame({'A': [True] * 3 + [False] * 5, 'B': np.random.rand(8) }) In [15]: df = df.sort_values('B') In [16]: df Out[16]: A B 6 False 0.037710 2 True 0.315414 4 False 0.332480 7 False 0.445505 3 False 0.580156 1 True 0.741551 5 False 0.796944 0 True 0.817563 I need something that will give me a new column with values

3 3 2 2 2 2 1 1 That is, for each row it should contain amount of True values on this row and rows below.

I've tried various methods using .iloc[::-1] but result is not that is desired.

It looks like I'm missing some obvious bit of information. I've starting using Pandas only yesterday.


Источник: https://stackoverflow.com/questions/378 ... -dataframe

Вернуться в «Python»