indexing several csv files with pandas from records?
-
29-10-2019 - |
سؤال
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:
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.
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.
لا يوجد حل صحيح