简体   繁体   中英

Pandas.read_csv reads all of the file into one column

I have a csv file in the form "...","...","..."... with over 40 columns. When I used this simple code, it only gives me one massive key. I've been messing with it for over an hour and I can't figure how to use pandas to fix this.

dataframe = pd.read_csv(filePath, header=0, encoding='iso-8859-1')
datakeys = dataframe.keys();
print(datakeys)

I fixed the issue by specifying names field of read_csv and header=None .

fields = ["colA", "colB"];
df = pd.read_csv("/tmp/data.csv", sep="|", header=None, names=fields)

I fixed the issue by writing a script to reformat the .csv There was a minor formatting issue in the .csv that was causing Panda to basically do nothing. Weird

This is an old question, but as I recently encountered the same issue, thought I post what fixed it for me:

The problem was setting the sep= argument incorrectly. When put the appropriate separator (double check your data file for the correct separator!), it read the file into the correct number of columns.

Maybe it helps others as well.

Since these answers did not satisfy me, but I found another way, I'm also posting my solution here:

So I was using "§" as separator and pandas-1.1.5 . However, the problem was the same as described by the question maker (apart from the separator). My solution was to update pandas:

pip3 install --upgrade pandas

After that, with

    df = pd.read_csv(input_file_path, sep="§", encoding='utf8', quotechar='"', engine="python")

everything went fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM