Replacing rows of dataframe with rows of another dataframe that have the same index

I have a dataframe that has rows with indices 0 to 128 and a smaller dataframe with indices 4, 8, 105, and 107.

I made edits to the rows in the smaller dataframe and am now trying to replace rows indexed 4, 8, 105, and 107 in the large dataframe with rows indexed 4, 8, 105, and 107 in the smaller dataframe.

Why can I not just do:

bigDF[smallDF.index] = smallDF

How would I accomplish this replacement? Thank you!

Topic dataframe pandas indexing python

Category Data Science


In your code, you passed smallDF.index directly. The interpreter considers this as a list of column names. So your code will be interpreted like below. But 4,8,105,107 are not columns. They are indexes.

bigDF[[4,8,105,107] = smallDF

To locate a row based on the index, use the loc function. Try using below.

bigDF.loc[smallDF.index]=smallDF

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.