문제

I have a list of csv files ("file1", "file2", ...") that have two columns but do not have header labels. I'd like to assign them header labels and them as a DataFrame which is indexed by file and then indexed by those column labels. For example, I tried:

import pandas

mydict = {}
labels = ["col1", "col2"]
for myfile in ["file1", "file2"]:
  my_df = pandas.read_table(myfile, names=labels)
  # build dictionary of dataframe records
  mydict[myfile] = my_df

test = pandas.DataFrame(mydict)

this produces a DataFrame, test, indexed by "myfile1", "myfile2"... however, I'd like each of those to be indexed by "col1" and "col2" as well. My questions are:

  1. how can I make it so the first index is file, and second index are columns I assigned (in the variable labels)? So that I can write:

    test["myfile1"]["col1"]

right now, test["myfile1"] only gives me a series of records.

  1. also, how can I then reindex things so that the first indices are the column labels of each file and the second is the filename? So that I can write:

    test["col1"]["myfile1"]

or print test["col1"] and then see the value of "col1" shown for myfile1, myfile2, etc.

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top