Home > Software design >  Python - Read Columns from CSV with Pandas
Python - Read Columns from CSV with Pandas

Time:09-29

I have a CSV File and I want to separate Columns to Lists in Python (with Pandas).

I already done this before with other CSVs, and nothing gone wrong.

This is the code i'm using:

from pandas import *

data = read_csv("newCSV Report.csv")

computer = data['Computer Name'].tolist()
user        = data['Computer Current User'].tolist()

print(computer)
print(user)

And this is my CSV File: (I covered the Rows for privacy)

enter image description here

Finally, this is the error I get if i run the code:

enter image description here

How I can solve this?

EDIT that might help to solve: If I print data before creating the two lists, that's the result: enter image description here

So Python recognizes only 1 Column instead of 2.

CodePudding user response:

Maybe try add sep = "\t" to data:

data = pd.read_csv("newCSV Report.csv", sep = "\t")

Your previous error was from invalid key, when you imported csv file You didn't have 2 separate columns cause of default setting sep=",". You was searching ['Computer Name'] and ['Computer Current User'] but there was just ['Computer Name\tComputer Current User'].

enter image description here

CodePudding user response:

Something similar happened with me. Try this once:

import pandas as pd

data = pd.read_csv("newCSV Report.csv", error_bad_lines=False)

computer = data['Computer Name'].tolist()
user        = data['Computer Current User'].tolist()

print(computer)
print(user)
  • Related