2

I have two pandas dataframes:

df1

   2014  2015  2016  2017
NY   90    85    84    80
IL   88    79    72    65
VA   75    76    81    79
MD   60    62    66    70

df2

   Empty DataFrame
   Columns: []
   Index: [CA, WA, NY, VA, DE, MD, IL]

How can I get the following dataframe using the two above?

   2014  2015  2016  2017
CA   -1    -1    -1    -1
WA   -1    -1    -1    -1
NY   90    85    84    80
VA   75    76    81    79
DE   -1    -1    -1    -1
MD   60    62    66    70
IL   88    79    72    65

1 Answer 1

5

Use reindex by index of df2:

df = df1.reindex(df2.index, fill_value=-1)
print (df)
    2014  2015  2016  2017
CA    -1    -1    -1    -1
WA    -1    -1    -1    -1
NY    90    85    84    80
VA    75    76    81    79
DE    -1    -1    -1    -1
MD    60    62    66    70
IL    88    79    72    65
Sign up to request clarification or add additional context in comments.

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.