Home > Enterprise >  Appending zeros for multiple Dataframes to be same size
Appending zeros for multiple Dataframes to be same size

Time:11-21

rows_len=[]
row_list=[]


rows_list= list(patientdataset.copy().values())
for i in range(0,len(rows_list)):
    rows_len.append(len(rows_list[i]))   


max_length = max(rows_len)

for lista in rows_list:
    for i in range(max_length - len(lista)):
        lista = pd.DataFrame(lista, columns=['Level','IsCorrect','TotalSeconds'])
        zeros = [0.0,0.0,0.0]
        zerosseries = pd.Series(zeros, index = ['Level','IsCorrect','TotalSeconds'])
        lista = lista.append(zerosseries, ignore_index=True)

So I have a list of Dataframes, rows_list, all Dataframes have the same 3 columns but vary in row sizes.

When I try to append a series of zero values to make the dataframes all same size it doesn't append (lista = lista.append(zerosseries, ignore_index=True)) and I've tried many other ways, does anyone have an idea how to fix this?

CodePudding user response:

Your code seems to be fine, just that you are not collecting the changes anywhere -

out_list = []
for lista in row_list:
    for i in range(max_length - lista.index.size):
        print(lista.index.size)
        zeros = [0.0, 0.0, 0.0]
        zeros_series = pd.Series(zeros, index=lista.columns)
        lista = lista.append(zeros_series, ignore_index=True)
    out_list.append(lista)

As you are looping with the variable lista in the outer for loop and then creating a new lista in the inner loop, the modified lista is not the same as the original one

  • Related