Настройка и предварительная обработка
import ast
import numpy as np
if isinstance(x.at[0, 'meta'], str):
df['meta'] = df['meta'].map(ast.literal_eval)
str.get
с Series.argsort
df.iloc[df['meta'].str.get('total_expense').astype(int).argsort()]
id import_id investor_id loan_id meta
2 35739 unremit_loss_100314 Q06 51765141 {'total_paid': '80', 'total_expense': '65'}
0 35736 unremit_loss_100312 Q05 51765139 {'total_paid': '75', 'total_expense': '75'}
1 35737 unremit_loss_100313 Q06 51765140 {'total_paid': '77', 'total_expense': '78'}
Понимание списка
df.iloc[np.argsort([int(x.get('total_expense', '-1')) for x in df['meta']])]
id import_id investor_id loan_id meta
2 35739 unremit_loss_100314 Q06 51765141 {'total_paid': '80', 'total_expense': '65'}
0 35736 unremit_loss_100312 Q05 51765139 {'total_paid': '75', 'total_expense': '75'}
1 35737 unremit_loss_100313 Q06 51765140 {'total_paid': '77', 'total_expense': '78'}
Если вам нужно обработать NaNs / пропущенные данные, используйте
u = [
int(x.get('total_expense', '-1')) if isinstance(x, dict) else -1
for x in df['meta']
]
df.iloc[np.argsort(u)]
id import_id investor_id loan_id meta
2 35739 unremit_loss_100314 Q06 51765141 {'total_paid': '80', 'total_expense': '65'}
0 35736 unremit_loss_100312 Q05 51765139 {'total_paid': '75', 'total_expense': '75'}
1 35737 unremit_loss_100313 Q06 51765140 {'total_paid': '77', 'total_expense': '78'}