0
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

1
  • 2
    df.to_csv('filename.csv') Commented Oct 21, 2020 at 15:07

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.