3 回答

TA貢獻(xiàn)1906條經(jīng)驗 獲得超10個贊
干得好
df.id.str.len().value_counts()
1 3
3 2
2 1
Name: id, dtype: int64

TA貢獻(xiàn)1811條經(jīng)驗 獲得超6個贊
一個麻木的解決方案:
np.transpose(np.unique(df.id.map(len), return_counts=True))
Out[229]:
array([[1, 3],
[2, 1],
[3, 2]], dtype=int64)
創(chuàng)建數(shù)據(jù)框
pd.DataFrame(np.transpose(np.unique(df.id.map(len), return_counts=True)),
columns=['str_length', 'count'])
Out[231]:
str_length count
0 1 3
1 2 1
2 3 2

TA貢獻(xiàn)1799條經(jīng)驗 獲得超6個贊
使用 groupby 和計數(shù)。
(
df.groupby(by=df.id.apply(len))
.id.count()
.to_frame('count')
.rename_axis(index='str_length')
.reset_index()
)
str_length count
0 1 3
1 2 1
2 3 2
添加回答
舉報