1

I originally have a 'monthly' DataFrame with months (1-11) as column index and number of disease cases as values. I have another 'disease' DataFrame with the first 2 columns as 'Country' and 'Province'. I want to combine the 'monthly' DataFrame with the 2 columns, and the 2 columns should be still be the first 2 columns in the combined 'monthly' DataFrame (Same index position).

In other words, the original 'monthly' DataFrame is:

     1   2   3   4   5   6   7   8    9    10   11
 0   1   5   8   0   9   9   8   18   82   89   81
 1   0   1   9   19  8   12  29  19   91   74   93

The desired output is:

    Country       Province    1   2   3   4   5   6   7   8    9    10   11
 0  Afghanistan  Afghanistan  1   5   8   0   9   9   8   18   82   89   81
 1  Argentina    Argentina    0   1   9   19  8   12  29  19   91   74   93

I was able to append the 2 columns into the 'monthly' DataFrame by this code:

monthly['Country'] = disease['Country']
monthly['Province'] = disease['Province']

However, this puts the 2 columns at the end of the 'monthly' DataFrame.

      1   2   3   4   5   6   7   8    9    10   11   Country       Province
 0    1   5   8   0   9   9   8   18   82   89   81   Afghanistan  Afghanistan
 1    0   1   9   19  8   12  29  19   91   74   93   Argentina    Argentina

How should I improve the code without using the insert() function ? Can I use the iloc to specify the index position?

Thanks for your help in advance!

cwyjm
  • 61
  • 7

1 Answers1

1

Use concat with selecting first 2 columns by positions by DataFrame.iloc, here first : means get all rows:

df = pd.concat((disease.iloc[:, :2], monthly), axis=1)

Or by columns names:

df = pd.concat((disease[['Country','Province']], monthly), axis=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252