It seems the default for pd.read_csv()
is to read in the column names as str
. I can't find the behavior documented and thus can't find where to change it.
Is there a way to tell read_csv()
to read in the column names as integer?
Or maybe the solution is specifying the datatype when calling pd.DataFrame.to_csv()
. Either way, at the time of writing to csv, the column names are integers and that is not preserved on read.
The code I'm working with is loosely related to this (credit):
df = pd.DataFrame(index=pd.MultiIndex.from_arrays([[], []]))
for row_ind1 in range(3):
for row_ind2 in range(3, 6):
for col in range(6, 9):
entry = row_ind1 * row_ind2 * col
df.loc[(row_ind1, row_ind2), col] = entry
df.to_csv("df.csv")
dfr = pd.read_csv("df.csv", index_col=[0, 1])
print(dfr.loc[(0, 3), 6]) # KeyError
print(dfr.loc[(0, 3), "6"]) # No KeyError