[英]Select rows and subrows in pandas multiindex dataframe by condition
我有一個多索引的數據框,它的索引和行中包含一些NaN
值。
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
是否有可能通過條件col3 == True
獲得該數據幀的子集,該條件還包括該條件所在行的所有“子行”?
當我去
print(df[df.col3 == True])
我懂了
col1 col2 col3
index1 index2
abc NaN some_value NaN True
條件所在的行。 但是,我正在尋找的是
col1 col2 col3
index1 index2
abc NaN some_value NaN True
NaN xyz NaN other value NaN
,包括本身沒有True
值但是index1 == abc
的行的“子行”的行。
那可能嗎? 還是數據幀搞砸了,應該以其他方式構造?
一個簡單的解決方案是僅在填充的col3
上使用一個條件,其中將NaNs
替換為它們所屬的行的值。 例如:
>>> df['col3'].fillna(method='pad')
index1 index2
abc NaN True
NaN xyz True
def NaN False
NaN uvw False
Name: col3, dtype: bool
現在,您可以應用以下條件:
>>> df[df['col3'].fillna(method='pad')]
col1 col2 col3
index1 index2
abc NaN some_value NaN True
NaN xyz NaN other_value NaN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.