'Count the number of missing values in each column and sort them

I am a very beginner. learning online courses. Can somebody please help me figure out what to fill in the blank? It required "Order them (increasing or decreasing) using sort_values". missing.sort_values(by=___)



Solution 1:[1]

If you want to sort with the missing numbers NaN first you can specify na_position='first' (the default value is last).

missing.sort_values(by=['column_name'] ,na_position='first')

Solution 2:[2]

Assuming "missing" is a python DataFrame, you can sort_values by a column.

For example,

>>> df = pd.DataFrame({
...     'col1': ['A', 'A', 'B', np.nan, 'D', 'C'],
...     'col2': [2, 1, 9, 8, 7, 4],
...     'col3': [0, 1, 9, 4, 2, 3],
...     'col4': ['a', 'B', 'c', 'D', 'e', 'F']
... })

>>> df
  col1  col2  col3 col4
0    A     2     0    a
1    A     1     1    B
2    B     9     9    c
3  NaN     8     4    D
4    D     7     2    e
5    C     4     3    F

>>> df.sort_values(by=['col1'])
>>> df

  col1  col2  col3 col4
0    A     2     0    a
1    A     1     1    B
2    B     9     9    c
5    C     4     3    F
4    D     7     2    e
3  NaN     8     4    D

For further reference, you can see the docs here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Harsh Jhunjhunwala