简体   繁体   中英

how to copy one column of a csv file to another csv file?

I have one *.csv file which has 250000 row and 16 column. I would like to copy two specific columns of this file to a new *.csv file with python. All the suggested codes have done this by writing a for loop. But as the data is big it is very slow with for loop. Can anyone help me how this is possible without loop?

The csv headers look likes this: Image_ID, Image_Class, Age, ...

import pandas as pd
import csv 

headers = 'ImageID, Image_class, Age, ...'
data = pd.read_csv(file, names=headers)
image_id_column = data.ImageID.tolist()
image_class_column = data.Image_class.tolist()

You can do something like this.

test = data.loc[:,['ImageID','Image_class']]
test.to_csv('test.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM