我有一个数据框users
,其中包含三列id
,username
和password
。
我正在尝试计算password
中少于8个字符数的行数。
我的代码:
# Calculating the length of user's passwords
users['length'] = users.password.str.len()
# Flagging the users with too short passwords`
users['too_short'] = pd.cut(users['length'], bins=[0,7,300], labels=[True,False])
print (users[users['too_short'].any(1)])
# Counting and printing the number of users with too short passwords
print(users['too_short'].count())
# Taking a look at the 12 first rows
users.head(12)
它出现以下错误:
TypeError: Categorical cannot perform the operation any
列too_short
的值为True
或False
。我试图找到一种方法来计算此列中基于布尔值的行数,但没有运气。
答案 0 :(得分:1)
试试这个:
users['too_short'] = users['length'] < 7
print(users[users['too_short']])
print(users['too_short'].count())