簡體   English   中英

Python Pandas-基於單個索引的具有多個索引的邏輯索引數據框

[英]Python Pandas - Logical indexing dataframe with multiple indexes based on single index

我對Pandas還是很陌生,但是我已經搜索了很多,卻找不到我想要的東西。

所以這是我的問題:

我有兩個數據框-一個具有多個索引,另一個僅具有一個索引

df1=

               value1  value2
ind1 ind2
a      1          1.1     7.1
b      2          2.0     8.0
c      3          3.0     9.0
a      4          4.0    10.0
b      5          5.0    11.0
c      6          6.0    12.0


df2=

           value1  value2
ind1
a           8.0     7.0
b           9.0     8.0
c           3.0     9.0
d           11.0   10.0
e           12.0    11.0
f           1.0    12.0

我想基於df2從df1索引數據,其中value1 > value2

df2['value1'] > df2['value2']

我知道我可以從df2獲取數據

df2.loc[df2['value1'] > df2['value2']]

但是我如何從df1獲取數據? 我試過了:

df1.loc[df2['value1'] > df2['value2']]

但是它失敗了

*** IndexingError: Unalignable boolean Series key provided

任何建議將不勝感激,謝謝!

從df2獲取索引,然后在這些索引上選擇df1:

indices = df2.loc[df2['value1'] > df2['value2']]
>>>indices
Index([u'b', u'd', u'e'], dtype='object')
>>>df1.ix[indices]

ind1 ind2   val1    val2
b    2      2.0     8.0
b    5      5.0     11.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM