根据一列的值和另一列的列表长度对数据框进行排序

时间:2019-07-19 14:13:15

标签: python pandas dataframe

在datframe中,例如:

<div id="wrapper">
  <div class="item-wrapper">
    <child-stub></child-stub>
  </div>
  <div class="item-wrapper">
    <child-stub></child-stub>
  </div>
</div>

如何对以上数据框进行排序,以使子女最少的员工排在首位?

Employee      list_of_children              child

Jonathen      ['John', 'Bob','Jennifer']    John

Jonathen      ['John', 'Bob','Jennifer']    Bob

Jonathen      ['John', 'Bob','Jennifer']    Jennifer

Emily         ['Clark']                     Clark

Hans          ['Watson', 'Hans Jr']         Watson

Hans          ['Watson', 'Hans Jr']         Watson

1 个答案:

答案 0 :(得分:2)

获得argsort后,我们可以使用len

df = df.iloc[df['list_of_children'].str.len().argsort()]