Search code examples
pythonpandascsvpycharm

Python how to write a new csv file using pandas


import pandas as pd
pd.__version__
df = pd.read_csv('myfile.csv', usecols=[0,3,4,5,9])
print (df)

I only want to return these specific columns from my csv file and write it into a new csv file?

How can I do this

so far i can read the data!! but not sure how to write it

ABSOLUTE PYTHON BEGGINER ALERT


Solution

  • based on your input, i assume you are trying to achieve something like this ?

    #import library
    import pandas as pd
    pd.__version__
    
    # read the csv with all columns
    df = pd.read_csv('myfile.csv')
    print (df)
    
    # create csv with filtered columns
    filter_cols = ["0","3","4","5","9"]
    df.to_csv('output.csv', columns = filter_cols)