Skip to content
Advertisement

Pandas apply same filtering on two dataframes

I have two pandas dataframes with the same indexes and lengths. However, they might contain nans at different places. See, example input below:

JavaScript
JavaScript

I want to do some calculations where I want to remove indexes in both the dataframes even if one of them has a NaN values. So, in those case only the first row is valid.

Is there a way to do this easily in pandas?

Advertisement

Answer

You can merge to get the common indices, then subset:

JavaScript

Alternative, you can use python sets to get the intersection:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement