0

I'm trying to convert a CSV file into Python list I have strings organize in columns. I need an Automation to turn them into a list. my code works with Pandas, but I only see them again as simple text.

import pandas as pd
data = pd.read_csv("Random.csv", low_memory=False)
dicts = data.to_dict().values()
print(data)

so the final results should be something like that : ('Dan', 'Zac', 'David')

TaboliD
  • 1
  • 1

2 Answers2

0

You can simply do this by using csv module in python

import csv
with open('random.csv', 'r') as f:
    reader = csv.reader(f)
    your_list = map(list, reader)

print your_list

You can also refer here

Ishaan
  • 1,249
  • 15
  • 26
0

If you really want a list, try this:

import pandas as pd
data = pd.read_csv('Random.csv', low_memory=False, header=None).iloc[:,0].tolist()

This produces

['Dan', 'Zac', 'David']

If you want a tuple instead, just cast the list:

data = tuple(pd.read_csv('Random.csv', low_memory=False, header=None).iloc[:,0].tolist())

And this produces

('Dan', 'Zac', 'David')

I assumed that you use commas as separators in your csv and your file has no header. If this is not the case, just change the params of read_csv accordingly.

DocDriven
  • 3,726
  • 6
  • 24
  • 53
  • Thank you! you just didn't mention ''import pandas'' first! im all new to programming. anyway that solved my issues – TaboliD Jan 15 '19 at 14:21
  • @TaboliD: My bad, I will edit this immediately. I thought this was clear, as you have already imported it. – DocDriven Jan 15 '19 at 14:46