Skip to content
Advertisement

All cells getting updated in pandas df using loc

So I create an empty pandas df, where I initialize all the cell values to empty lists, except the diagonals, which are set to math.inf

The indexes are the start position, and the column headers are the end position

I want to get the start and end positions, and the difference between the days to get from start to end, and put that value in df.loc[start, end] by using append. But for some reason, every single cell in the df is getting updated, and i dont know why

My code is shown below

JavaScript

part of the output of the df is shown below:

JavaScript

So for the first example,

start = Action required: Submit requested documents

end = At NVC

diff = 47

So i want it to store just 47 as a list in df[ar_doc][nvc]. But it is storing the difference of all days in all the cells

Why does this happen and how to fix it?

Advertisement

Answer

All your pandas data are referencing the same list. You should change how you initialize the DataFrame. You should create a new list in each cell.

Try:

JavaScript

Separately, since you’re already using pandas, you don’t need to use datetime to parse dates. You can reduce your loop to the following:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement