[Python] pandas Iterate columns and rows in a dataframe to get the output straing of the...

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe like below

    data = {'col7': ['Apple','Banana','-','-','-','-'],'col6': [float('NaN'),float('NaN'),'-','-','-','HA'],'col5': [float('NaN'),float('NaN'),'-','-','-','HB'],'col4': [float('NaN'),float('NaN'),'-','Egg','G1','HC'],'col3': [float('NaN'),'Cat','Dog',float('NaN'),'G2','HD'],'col2': [float('NaN'),float('NaN'),float('NaN'),float('NaN'),'G3',float('NaN')],'col1': [float('NaN'),float('NaN'),float('NaN'),float('NaN'),'G4','HD'],'col0': [float('NaN'),float('NaN'),float('NaN'),'Fish','G5',float('NaN')]}
    df = pd.DataFrame(data)


    [​IMG]

    I'd like to print the string for iteration columns and rows with column name and also store in a list

    [​IMG]

    A For loop to print the string and save in a list :

    Apple=output[col7:col0]

    Banana=output[col7:col4]

    Cat=output[col3:col0]

    Dog=output[col3:col0]

    .....

    Appriciated your big help!!

    Continue reading...

Compartilhe esta Página