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']
                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.
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.
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.
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]
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With