我有一个带有唯一索引和“用户”、“tweet_time”和“tweet_id”列的数据框。
我想计算每个用户重复的 tweet_time 值的数量。
users = ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C']
tweet_times = ['01-01-01 01:00', '02-02-02 02:00', '03-03-03 03:00', '09-09-09 09:00',
'04-04-04 04:00', '04-04-04 04:00', '05-05-05 05:00', '09-09-09 09:00',
'06-06-06 06:00', '06-06-06 06:00', '07-07-07 07:00', '07-07-07 07:00']
d = {'users': users, 'tweet_times': tweet_times}
df = pd.DataFrame(data=d)
期望的输出
一个:0
乙:1
C: 2
我设法使用下面的代码获得所需的输出(A: 0 除外)。但是是否有更 pythonic/更有效的方法来做到这一点?
# group by both columns
df2 = pd.DataFrame(df.groupby(['users', 'tweet_times']).tweet_id.count())
# filter out values < 2
df3 = df2[df2.tweet_id > 1]
# turn multi-index level 1 into column
df3.reset_index(level=[1], inplace=True)
# final groupby
df3.groupby('users').tweet_times.count()
最佳答案
我们可以使用crosstab
创建一个频率表,然后检查大于 1
的计数,创建一个 bool 掩码,然后沿着 axis=1
sum
这个掩码
pd.crosstab(df['users'], df['tweet_times']).gt(1).sum(1)
users
A 0
B 1
C 2
dtype: int64
关于python - 如何使用 Pandas 计算另一列中每个值在一列中的出现次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67489092/