0

How can i store data of a specific column in a csv file for example Name, in a list using python? When i try to output It output repeatedly enter image description here

Please help

1
  • Please format your picture to be visible for other than a link to your code snippet. Or you can add your code snippets in " ``` " enclosing quotation marks. For more on how to add picture check here meta.stackoverflow.com/a/344853/6021740 Commented Mar 2, 2021 at 20:25

3 Answers 3

1

The easiest way is to use pandas:

import pandas as pd

df = pd.read_csv('names.csv')
names = df['Name'].tolist()
Sign up to request clarification or add additional context in comments.

Comments

1

One possibility would be to use a list comprehension.

import csv

with open("names.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=",")

    # List comprehension
    csv_list = [line[0] for line in csv_reader]

The "0" in line[0] can be changed to be whichever column is desired.

Comments

0

One way would be to use pandas.

import pandas as pd

df = pd.read_csv('filepath_here')

your_list = df['Name'].tolist()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.