【问题标题】:Multi-Column Pandas counting based on multiple criteria基于多个标准的多列 Pandas 计数
【发布时间】:2017-10-24 20:00:58
【问题描述】:

我有一个我想在下面计算的“单词”列表

word_list = ['one','two','three']

我在 pandas 数据框中有一列,下面有文字。

TEXT                                       | USER    | ID
-------------------------------------------|---------|------
"Perhaps she'll be the one for me."        | User 1  | 100
"Is it two or one?"                        | User 1  | 100
"Mayhaps it be three afterall..."          | User 2  | 150
"Three times and it's a charm."            | User 2  | 150
"One fish, two fish, red fish, blue fish." | User 2  | 150
"There's only one cat in the hat."         | User 3  | 200
"One does not simply code into pandas."    | User 3  | 200
"Two nights later..."                      | User 1  | 100
"Quoth the Raven... nevermore."            | User 2  | 150

我想要的输出如下所示,我想使用“TEXT”列中的数据计算与 word_list 中的任何单词相关的文本的唯一用户数。在计算了唯一用户之后,我还想计算与每条推文相关的关注者总和,与单词的唯一用户数相关联。

Word | Unique User Count | ID Sum
one  |      3            | 450
two  |      2            | 250
three|      1            | 150

有没有办法在 Python 2.7 中做到这一点?

【问题讨论】:

    标签: python python-2.7 pandas


    【解决方案1】:

    我分解步骤

    df.columns=['TEXT','USER','ID']
    
    df[word_list]=df.TEXT.str.lower().apply(lambda x : pd.Series([x.find(y) for y in word_list])).ne(-1)
    df1=df[['USER','one','two','three','ID']].set_index(['USER','ID']).astype(int).replace({0:np.nan})
    Target=df1.stack().reset_index().groupby('level_2').agg({'USER':lambda x : len(set(x)),'ID':lambda x : sum(set(x))})
    Target=Target.reset_index()
    Target.columns=['Word','Unique User Count','ID Sum']
    Target
    Out[97]: 
        Word  Unique User Count  ID Sum
    0    one                  3     450
    1  three                  1     150
    2    two                  2     250
    

    【讨论】:

    • 第 3 行:KeyError: 'level_2'
    • @Leggerless 在开头添加这个 df.columns=['TEXT','USER','ID'],你的列名包含空白
    • @Leggerless df1.stack().reset_index() 输入这个,告诉我你的列名
    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多