[Python] Filter pandas dataframe with specific column names in python

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

  1. Stack

    Stack Membro Participativo

    I have a pandas dataframe and a list as follows

    mylist = ['nnn', 'mmm', 'yyy']
    mydata =
    xxx yyy zzz nnn ddd mmm
    0 0 10 5 5 5 5
    1 1 9 2 3 4 4
    2 2 8 8 7 9 0


    Now, I want to get only the columns mentioned in mylist and save it as a csv file.

    i.e.

    yyy nnn mmm
    0 10 5 5
    1 9 3 4
    2 8 7 0


    My current code is as follows.

    mydata = pd.read_csv( input_file, header=0)

    for item in mylist:
    mydata_new = mydata[item]

    print(mydata_new)
    mydata_new.to_csv(file_name)


    It seems to me that my new dataframe produces wrong results.Where I am making it wrong? Please help me!

    Continue reading...

Compartilhe esta Página