0

I have 3 datasets which I need to join together by Country name:

merged_df = pd.merge(Energy, GDP, on="Country")
merged_df2 = pd.merge(merged_df, ScimEn, on="Country")
merged_df2.set_index('Country')

The assignment says that I have to:

  1. Select only specific colunmns
  2. Sort by rank
  3. Only take the first 15 rows based on rank.

so I did this:

 df3 = merged_df2[['Country','Rank' ,'Documents', 'Citable documents', 'Citations', 'Self-citations', 'Citations per document', 'H index', 'Energy Supply', 'Energy Supply per Capita', '% Renewable', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']]
    df3.set_index('Country')
    df4 = df3[['Country','Rank' ,'Documents', 'Citable documents', 'Citations', 'Self-citations', 'Citations per document', 'H index', 'Energy Supply', 'Energy Supply per Capita', '% Renewable', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']]
    
    df4 = df4.sort_values(by=['Rank'], ascending=True)
    df4.set_index('Country')
  
    print(df4.index)

and it prints:

Int64Index([3, 14, 9, 13, 11, 2, 5, 6, 4, 10, 8, 12, 7, 0, 1], dtype='int64')

but it should print 1,1,2,3,4..15

what am I missing?

2 Answers 2

2

You need assign back:

df4 = df4.set_index('Country')

Or:

df4.set_index('Country', inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

1

You need to set the inplace argument to true.

df3.set_index('Country',inplace = True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.