Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read in the first column of a CSV in Python

Tags:

python

csv

I have a CSV (mylist.csv) with 2 columns that look similar to this:

jfj840398jgg     item-2f
hd883hb2kjsd     item-9k
jie9hgtrbu43     item-12
fjoi439jgnso     item-3i

I need to read the first column into a variable so I just get:

jfj840398jgg
hd883hb2kjsd
jie9hgtrbu43
fjoi439jgnso

I tried the following, but it is only giving me the first letter of each column:

import csv
list2 = []
with open("mylist.csv") as f:
    for row in f:
        list2.append(row[0])

So the results of the above code are giving me list2 as:

['j', 'h', 'j', 'f']
like image 737
P.J. Avatar asked Sep 23 '16 14:09

P.J.


People also ask

How do I read the first column in Python?

Use iloc[] to select first column of pandas dataframe. Use [] to select first column of pandas dataframe. Use head() to select first column of pandas dataframe. Get first column of pandas dataframe as list on python.

How do I read the first row of a CSV file in Python?

Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open() method. Step 2: Create a reader object by passing the above-created file object to the reader function. Step 3: Use for loop on reader object to get each row.

How do I read a specific column in a DataFrame in Python?

You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.


2 Answers

You should split the row and then append the first item

list2 = []
with open("mylist.csv") as f:
    for row in f:
        list2.append(row.split()[0])

You could also use a list comprehension which are pretty standard for creating lists:

with open("mylist.csv") as f:
    list2 = [row.split()[0] for row in f]
like image 192
Moses Koledoye Avatar answered Sep 27 '22 17:09

Moses Koledoye


You can also use pandas here:

import pandas as pd
df = pd.read_csv(mylist.csv)

Then, getting the first column is as easy as:

matrix2 = df[df.columns[0]].as_matrix()
list2 = matrix2.tolist()

This will return only the first column in list. You might want to consider leaving the data in numpy, if you're conducting further data operation on the result you get.

like image 44
alpaca Avatar answered Sep 27 '22 16:09

alpaca