Reputation: 305
I have this data example:
0 1 2 3 4 5
2 Text1 1 121 2.600,01 10
3 Text2 87 44 0 0,00
4 Text3 19 33 0 0,00
5 SUM 6 0 0 0 0 0,00
as you can see df takes different sizes (amount of data in rows)
I would like the last column (which depends on the longest row) not to be empty in each case, i.e. if any row is shorter than the longest one, in this case the last column should have the same value as the last occurring element, and the output should look like this:
0 1 2 3 4 5
2 Text1 1 121 2.600,01 10 -------------> 10
3 Text2 87 44 0 0,00 ------------> 0,00
4 Text3 19 33 0 0,00 ------------> 0,00
5 SUM 6 0 0 0 0 0,00
I need it to calculate the sum of the last possible column
I tried to do it at the stage of creating the list - if the list elements are smaller than the longest list item, then I inserted a character or anything and after that I will add a value on the last element of the list - the problem is that I don't know how to get the last numerical value from the list, that's why maybe it can be done somehow from df 'level'? Give me some hints or instructions how I can do it and whether it is possible at all.
Upvotes: 1
Views: 214
Reputation: 862921
If empty values are missing values NaN
s assign forward filling missing values by ffill
with selecting last column by position to last column:
df.iloc[:, -1] = df.ffill(axis=1).iloc[:, -1]
If empty values are empty strings:
df.iloc[:, -1] = df.replace('', np.nan).ffill(axis=1).iloc[:, -1]
Upvotes: 1