I am converting two columns of a pandas dataframe to a dict, but when I print the dict, it doesn't just print the columns requested

Viewed 43

I have the following dataframe (sample):

         Jan     Feb     Mar     Apr  ...     Oct       Nov       Dec        sum
Year                                  ...                                       
1961   45119   44969   45051   44997  ...   45716   45931.0   46035.0   544752.0
1962   46040   46309   46375   46679  ...   46927   46910.0   46901.0   559836.0
1963   46912   47000   47077   47316  ...   47805   47771.0   47863.0   569092.0

When I create a dictionary using the year and sum columns:

bls_data = pd.read_csv(file , header = 5, index_col = 0)
bls_data['sum'] = bls_data.sum(axis=1)
yearTotal = bls_data.to_dict('sum')

It doesn't import the requested data, and for some reason does it as the following:

{'Jan': Year
1961     45119
1962     46040
1963     46912

This is repeated for each month, and the sum giving me thirteen copies of the same numbers and for the life of me, I can't figure out why. It should just use the index (which is the year column) as the key and the sum column as the value but apparently that isn't what is happening. What am I missing? I have also tried using the following to perform the transition:

yearTotal = bls_data.set_index('Year').T.to_dict('sum')[0]

However, it says that there is no index "Year".

0 Answers
Related