【问题标题】:Python/Pandas: Idx and ixPython/熊猫:Idx 和 ix
【发布时间】:2017-06-27 21:29:06
【问题描述】:

当我尝试使用 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

【问题讨论】:

    标签: python pandas indexing minimum


    【解决方案1】:

    这应该会有所帮助:

    closest.to_frame().T
    

    或:

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

    【讨论】:

    • 非常感谢迈克!
    猜你喜欢
    • 2017-10-05
    • 2020-01-19
    • 2019-08-28
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多