I have a multi-indexed dataframe which contains some NaN values inside its index and rows.
In:
import pandas as pd
import numpy as np
row1 = {'index1' : 'abc', 'col1' : 'some_value', 'col3' : True}
row2 = {'index2' : 'xyz', 'col2' : 'other_value', 'col3' : np.nan}
row3 = {'index1' : 'def', 'col1' : 'different_value', 'col3' : False}
row4 = {'index2' : 'uvw', 'col2' : 'same_value', 'col3' : np.nan}
df = pd.DataFrame([row1, row2, row3, row4])
df.set_index(['index1', 'index2'], inplace=True)
print(df)
Out:
col1 col2 col3
index1 index2
abc NaN some_value NaN True
NaN xyz NaN other_value NaN
def NaN different_value NaN False
NaN uvw NaN same_value NaN
Is there a possibility to get a subset of that dataframe by the condition col3 == True which also includes all "subrows" of the row where that condition holds?
When I go for
print(df[df.col3 == True])
I get
col1 col2 col3
index1 index2
abc NaN some_value NaN True
which is the row where the condition holds. However, what I am looking for is
col1 col2 col3
index1 index2
abc NaN some_value NaN True
NaN xyz NaN other value NaN
, including the row which does not have the True value itself but is a "subrow" of the row with index1 == abc.
Is that possible? Or is the dataframe messed up and should be structured in a different way?