Python/Pandas: idx 和 ix

Python/Pandas: Idx and ix

当我尝试使用 Pandas 为列中的最小值编制索引时,它会将我的 DataFrame 转换为一个对象,而不是只有一行的 DataFrame。我想恢复到 DataFrame,即使它只是一行。

for j in Amount_ref:
    group['helper'] = np.abs(group['Amount'] - j)
    closest = group.ix[group['helper'].idxmin()]
    print(closest)

这是我得到的输出:

LoanAgreementID    E2502C04-DBC3-E611-8126-06CAB7997043
Amount                                           566.12
TransactionDate              2016-12-16 14:00:20.243000
ContactID          001A29DC-9253-E611-8126-06CAB7997043
PaymentType                                         NaN
CashLedgerType                                        5
KeyValue_String                                  Cheque
KeyValue_String                                    None
AutoNumber                                        54980
IssueDate                           2016-12-15 00:00:00
helper                                                0
Name: 2, dtype: object

这应该有帮助:

closest.to_frame().T

或:

index = group['helper'].idxmin()
closest = group.loc[index:index]