Trying to Drop Null Values Pertaining to Index in Multi-level Index
I'm trying to drop all IDs (index level 1) that contain any nulls in the "Data" column.
As an example, I've created a sample dataframe below:
import pandas as pd
import numpy as np
ids = ['0', '0', '0', '0', '0', '1','1','1','1','1','2','2','2','2','2','3','3','3','3','3']
dates = ['1/1/21', '1/2/21', '1/3/21', '1/4/21', '1/5/21','1/1/21', '1/2/21', '1/3/21', '1/4/21', '1/5/21','1/1/21', '1/2/21', '1/3/21', '1/4/21', '1/5/21','1/1/21', '1/2/21', '1/3/21', '1/4/21', '1/5/21']
data = [np.nan, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, np.nan, 16, 17, 18, 19]
df = pd.DataFrame(data=data, index=[ids,dates], columns=['Data'])
I'm looking to clean the dataframe to effectively return just the rows pertaining where ID is 1 or 2 because those are the only IDs with no null values for any of the dates in the second level of the index.
I've tried df.dropna(subset=['Data'], inplace=True), but that only drops the rows with null values, not the entire index.
What is the best way to drop all rows pertaining to an index if any of those rows has a null value in a Pandas Dataframe?
df.loc[~df.groupby(level=0).Data.transform(lambda x: x.isna().any())]