[Python] What am i doing wrong appending lists in pandas?

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have two csv files in the folder. My output for each loop iteration is avg_mtf. I have been successful in creating lists and concatenating them in the other loops within glob but my comb list is generating only 1 index. What am I doing wrong? Please help.

    import pandas as pd
    import glob
    file_list=glob.glob('*.csv')
    print (file_list);
    for file in file_list:
    data=pd.read_csv(file,encoding='cp1252', index_col=False, header=None)
    indices=data[(data[0].str.contains('Results=3DTiltCorrMTF@Best'))&(data[0].str.contains('Tangential'))] # dataframe with all rows containing Tangential
    a=indices.index.values
    tan_data=[]
    sag_data=[]
    comb
    x_axis=[]
    n=a[0]+1
    n=data.loc[n]
    x=n.str.split(r':| ', expand=True)
    x=x.iloc[:,1:-1]
    x=x.T.reset_index(drop=True)
    x=x.iloc[1:,:]
    x=x.reset_index(drop=True)
    del n

    for i in a:
    n=data.loc[i+2]
    n=n.str.split(' ', expand=True)
    n=n.iloc[:,1:-1]
    tan_mtf=n.T
    tan_data.append(tan_mtf)
    tan_mtf=pd.concat(tan_data,axis=1,ignore_index=True)
    tan_mtf=tan_mtf.reset_index(drop= True)

    del i,n,a

    indices=data[(data[0].str.contains('Results=3DTiltCorrMTF@Best'))&(data[0].str.contains('Sagittal'))]
    a=indices.index.values
    for i in a:
    n=data.loc[i+2]
    n=n.str.split(' ', expand=True)
    n=n.iloc[:,1:-1]
    sag_mtf=n.T
    sag_data.append(sag_mtf)
    sag_mtf=pd.concat(sag_data,axis=1,ignore_index=True)
    sag_mtf=sag_mtf.reset_index(drop= True)


    tan_mtf=tan_mtf.astype(float)
    sag_mtf=sag_mtf.astype(float)
    avg_mtf=(tan_mtf+sag_mtf)/2
    comb.append(avg_mtf)

    Continue reading...

Compartilhe esta Página