Goal is to create a function able to de-cumulate the column of table. For example:
data_matrix = {'x0': [0, 2, 0],
'x1':[2, 3, 1],
'x3':[4, 4, 1],
'x4':[6, 5, 1]}
data_mtrice = pd.DataFrame(data_matrix)
data_matrice = data_mtrice.T
That produces:
data_matrice.head()
Out[30]:
0 1 2
x0 0 2 0
x1 2 3 1
x3 4 4 1
x4 6 5 1
Each line is a cumulative sum. For example, 0+2= 2 then 0+2+2 = 4 then 0+2+2+2 = 6. I am looking for a function to de-cumulate. I tried to write:
import pandas as pd
def decumule(tableau):
decu_table = np.zeros(tableau.shape)
for ligne, element in enumerate(tableau.iloc()):
print("ligne = ",ligne)
for colon, elem in enumerate(tableau.iloc[ligne]):
if ligne > 0:
print("colonn",colon)
decu_table.iloc[[ligne, colon]] = tableau.iloc[[ligne, colon]] - tableau.iloc[[ligne - 1, colon]]
else:
decu_table.iloc[[ligne, colon]] = tableau.iloc[[ligne, colon]]
return decu_table
tentative = data_matrice.apply(lambda tableau: decumule(tableau))
That produces:
for colon, elem in enumerate(tableau.iloc[ligne]):
TypeError: 'numpy.int64' object is not iterable
Do you have any idea what can go wrong?
Regards, Atapalou