0

I have constructed a matrix with integer values for columns and index. The matrix is acutally hierachical for each month. My problem is that the indexing and selecting of data does not work anymore as before when I write the data to csv and then load as pandas dataframe.

Selecting data before writing and reading data to file:

matrix.ix[1][4][3] would for example give 123

In words select, month January and get me the (travel) flow from origin 4 to destination 3.

After writing and reading the data to csv and back into pandas, the original referencing fails but if I convert the column indexing to string it works:

matrix.ix[1]['4'][3]

... the column names have automatically been tranformed from integer into string. But I would prefer the original indexing. Any suggestions?

My current quick fix for handling the data after loading from csv is:

#Writing df to file
mulitindex_df_Travel_monthly.to_csv(r'result/Final_monthly_FlightData_countrylevel_v4.csv')


#Loading df from csv
test_matrix = pd.read_csv(filepath_inputdata+'/Final_monthly_FlightData_countrylevel_v4.csv', 
                                       index_col=[0, 1])


test_matrix.rename(columns = int, inplace = True) #Thx, @ayhan

CSV FILE: https://www.dropbox.com/s/4u2opzh65zwcn81/travel_matrix_SO.csv?dl=0

example df

Philipp Schwarz
  • 18,050
  • 5
  • 32
  • 36

2 Answers2

2

You could also do

df.columns = df.columns.astype(int)

or

df.columns = df.columns.map(int)

Related: what is difference between .map(str) and .astype(str) in dataframe

bers
  • 4,817
  • 2
  • 40
  • 59
1

I used something like this:

df = df.rename(columns={str(c): c for c in columns})

where:

df is pandas dataframe and columns are column to change

wailord
  • 377
  • 3
  • 5