简体   繁体   中英

How to read column names from csv as integer in pandas

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

我的临时解决方案是:

dfr.columns = dfr.columns.map(int)

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