Get a list with the contents of a column of a csv file

1

I'm looking to access all the values in the ddomain field in my list and create another new list with those values. I think that with a for item in your_list it could be done but I can not find the form.

with open("Top20_Media_10-12-17.csv", 'rb') as f:
    reader = csv.reader(f)
    your_list = list(reader)
    print your_list
[['Category', 'ddomain', 'opportunities', 'impressions', 'fillRate', 'DATA'], [' ', 'App', '69491941', '13720', '0.02', 'OK'], ['Real Site', 'kimcartoon.me', '157036', '650', '0.41', 'OK'], ['Real Site', 'capitalgazette.com', '2380', '450', '18.91', 'OK'], ['Real Site', 'best.io', '18180', '376', '2.07', 'OK'], ['Real Site', 'statsroyale.com', '121269', '373', '0.31', 'OK'], ['Detected Only', 'dingit.tv', '1019807', '359', '0.04', 'OK'], ['Real Site', 'hgtv.com', '6478', '274', '4.23', 'OK'], ['Real Site', 'mail.yahoo.com', '408744', '240', '0.06', 'OK'], ['Real Site', 'firstrowsportes.net', '211061', '238', '0.11', 'OK'], ['Real Site', 'tpc.googlesyndication.com', '314740', '232', '0.07', 'OK'], ['Real Site', 'bitlanders.com', '21827', '229', '1.05', 'OK'], ['Real Site', 'go.rivosport.co', '326261', '224', '0.07', 'OK'], ['Real Site', 'myasiantv.se', '100480', '220', '0.22', 'OK'], ['Real Site', 'firstrows.xyz', '145247', '218', '0.15', 'OK'], ['Real Site', 'streaming-foot.xyz', '204521', '200', '0.1', 'OK'], ['Real Site', 'start.att.net', '250292', '180', '0.07', 'OK'], ['Real Site', 'myp1p.eu', '30250', '155', '0.51', 'OK'], ['Real Site', 'spotify.com', '15037', '139', '0.92', 'OK'], ['Real Site', 'nflstream.net', '121400', '129', '0.11', 'OK'], ['Real Site', 'metoffice.gov.uk', '69592', '93', '0.13', 'OK']]
    
asked by Martin Bouhier 11.12.2017 в 20:07
source

1 answer

2

You must iterate with a for as you comment on the list of lists but you must obtain each element through indexing (in this case the column ddomain has the index 1, remembering that it is indexed from 0). Use a list by compression:

ddomain = [row[1] for row in your_list]

If you are only interested in this column, do not create the intermediate list, iterate directly over the rows:

with open("Top20_Media_10-12-17.csv", 'rb') as f:
    reader = csv.reader(f)
    ddomain = [row[1] for row in reader]

With pandas it is also very simple using the to_list method of the series:

import pandas as pd

df = pd.read_csv("Top20_Media_10-12-17.csv")
ddomain = df['ddomain'].tolist()

In all the cases you will get as the first element the name of the column ("ddomain"), if you do not want it, to eliminate it you can simply do:

del(ddomain[0])
    
answered by 11.12.2017 / 21:14
source