2

データフレームの同じタプル内の他の複数の値に基づいて特定の値を選択する方法を見つけようとしています。データは次のようになります(現在のデータフレームからコピー)

    DealID      PropId LoanId   ServicerId    ServicerPropId
0   BAC98765      15   000015    30220144       010-002-001
1   BAC98765      16   000016    30220092       010-003-001
2   BAC98765      45   000045    30220155       010-045-001
3   BAC98765      48   000048    30220157       010-048-001

SQL用語で私が達成したいのはこれです:

Select ServicerPropId from dataframe
 where DealID = 'BAC98765' and ServicerId = '30220144'  

データをスライスするためにいくつかの異なる方法を試しましたが、複数の選択基準を機能させて1つの値のみを変数に返す方法を理解できないようです。

4

1 に答える 1

2
columns = ['DealID', 'PropId', 'LoanId', 'ServicerId', 'ServicerPropId']

d = [('A', [ 'BAC98765', '15', '000015', '30220144', '010-002-001']),
     ('B', [ 'BAC98765', '16', '000016', '30220092', '010-003-001']),
     ('C', [ 'BAC98765', '45', '000045', '30220155', '010-045-001']),
     ('D', [ 'BAC98765', '48', '000048', '30220157', '010-048-001']),]

D =  pandas.DataFrame.from_items(d, orient='index', columns=columns)

criterion1 = D['DealID'].map(lambda x: x == 'BAC98765' )
criterion2 = D['ServicerId'].map(lambda x: x == '30220144')

res = D[criterion1 & criterion2]['ServicerPropId']

を使用するmapと、任意の条件を設定できます。この場合、これをより簡単に行うことができます(DSMのコメントで指摘されているように)

res = D[(D['DealID'] == "BAC98765") & (D["ServicerId"] == "30220144")]['ServicerPropId']

これは

In [35]: print res
A    010-002-001
Name: ServicerPropId

In [36]: type(res)
Out[36]: pandas.core.series.Series

(ドキュメント)

于 2013-02-02T19:57:16.330 に答える