第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在不同的 DataFrame 上使用 DataFrame 過(guò)濾器

在不同的 DataFrame 上使用 DataFrame 過(guò)濾器

喵喵時(shí)光機(jī) 2022-06-07 19:09:59
我有兩個(gè)數(shù)據(jù)框:df1:ID var11 Foo2 Foo3 Foo4 Bardf2:ID var12 Foo3 Bar4 Bar5 Foo    6 Bar我為 df1 創(chuàng)建了一個(gè)過(guò)濾器,其中 var1 = 'Foo':foo_filter=df1['var1']=='Foo'當(dāng)應(yīng)用于 df1 時(shí)正確返回:df1_filtered=df1[foo_filter]print(df1_filtered)ID var11  Foo2  Foo3  Foo然后我想對(duì) df2 應(yīng)用相同的過(guò)濾器,輸出如下:print(df2_filtered)ID var12  Foo3  Bar但是我遇到了這個(gè)錯(cuò)誤:df2_filtered=df2[foo_filter]IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match有什么辦法可以用過(guò)濾器做到這一點(diǎn),還是我需要不同的方法?
查看完整描述

2 回答

?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

只有你需要:


如果 ID 是索引,則將 ID 作為列:


df1.reset_index(inplace=True) #if ID is the index

df2.reset_index(inplace=True) #if ID is the index

filtered_df1=df1[df1['var1'].eq('Foo')]

print(filtered_df1)


    ID var1

0   1  Foo

1   2  Foo

2   3  Foo

df2.loc[df2['ID'].isin(filtered_df1['ID'])]


   ID var1

0   2  Foo

1   3  Bar


查看完整回答
反對(duì) 回復(fù) 2022-06-07
?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

df1 = pd.DataFrame({"id":[1,2,3,4], "var1":['Foo', 'Foo', 'Foo', 'Bar']})

df2 = pd.DataFrame({"id":[2,3,4,5, 6], "var1":['Foo','Bar','Bar','Foo', 'Bar']})


def filter(x):

    return x == 'Foo'


df1[df1['var1'].apply(filter)]

    id  var1

0   1   Foo

1   2   Foo

2   3   Foo




df2[df2['var1'].apply(filter)]

    id  var1

0   2   Foo

3   5   Foo


查看完整回答
反對(duì) 回復(fù) 2022-06-07
  • 2 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)