首页 > 解决方案 > Most pandas-onic way of getting statistics about the length of lists (average lengh, highest length, etc.) in a pandas df column

问题描述

I would like to get statistics on the list lengths in a pandas df column, such average length, lowest, highest, standard deviation, etc.

Example:

import pandas as pd

dfp = pd.DataFrame(
    {'trial_num': [[1, 2, 3, 1, 2, 3], [3,4,6,7], [2,2]],
     'subject': [[11, 2, 2, 2],[2,2,7],[4]]
    }
)
dfp

Output:

    trial_num   subject
0   [1, 2, 3, 1, 2, 3]  [11, 2, 2, 2]
1   [3, 4, 6, 7]    [2, 2, 7]
2   [2, 2]  [4]

So for this dataframe, I would like stats on the trial_num and subject columns.

So something like

trial_num
Average: 4
High: 6
Low: 2
Stdev: 2

What I have tried

I have tried

dfp.describe()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-90-8a598dabea30> in <module>()
----> 1 dfp.describe()

6 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/algorithms.py in _value_counts_arraylike(values, dropna)
    748         # TODO: handle uint8
    749         f = getattr(htable, "value_count_{dtype}".format(dtype=ndtype))
--> 750         keys, counts = f(values, dropna)
    751 
    752         mask = isna(values)

pandas/_libs/hashtable_func_helper.pxi in pandas._libs.hashtable.value_count_object()

pandas/_libs/hashtable_func_helper.pxi in pandas._libs.hashtable.value_count_object()

TypeError: unhashable type: 'list'

The only solution I can think of is to use iterrows to calculation the mean, high, and low, then with the mean, use iterrows again to calculate the stdev

标签: pythonpandas

解决方案


您可以使用str.len来获取每行的列表长度。然后你可以使用.describe

s = dfp['trial_num'].str.len()
s.describe()


       trial_num
count        3.0
mean         4.0
std          2.0
min          2.0
25%          3.0
50%          4.0
75%          5.0
max          6.0

推荐阅读