Jack Twain
Jack Twain

Reputation: 6372

Reading a CSV that has a head row and column with Panda

I have a CSV file that represent a graph. The file is basically an adjacency matrix. The first row is the labels of the nodes and the first column is also the labels. I want to read this file as a Dataframe in Pandas but unable to understand how to process the row and column headers. I'm looking here but unable to understand how to do it: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html#pandas.io.parsers.read_csv but couldn't

Upvotes: 0

Views: 704

Answers (1)

ericmjl
ericmjl

Reputation: 14704

I believe your post needs a bit more clarity in what you're trying to do, specifically, code that illustrates what you've tried, but I will update this answer as we go along and get more clarity. Therefore, here are some ideas to start you off.

If you do:

pandas.read_csv('put_file_name_here.csv')

You should be able to read in the CSV file.

Pandas automatically recognizes the first line as the column headers. Pandas was designed for tabular data, and not adjacency matrices. Therefore, you would likely have to specify that index_col=0 (i.e. the first column in the table). To do this, you will have to do:

pandas.read_csv('file_name.csv', index_col=0)

If you put more details on how your CSV file looks like, I will update this answer with more examples specific to your situation. Also, error messages (if you have any) will be very helpful.

Upvotes: 1

Related Questions