简体   繁体   English

Groupby 和 count 具有多个值的列

[英]Groupby and count columns with multiple values

Given this dataframe:鉴于此数据框:

df = pd.DataFrame({
    "names": [["Kevin, Jack"], ["Antoine, Mary, Johanne, Iv"], ["Ali"]],
    "commented": [["Kevin, Antoine, Iv"], ["Antoine, Mary, Ali"], ["Mary, Jack"]],
}, index=["1", "2", "3"])

that'll look like this:看起来像这样:

    names   commented
1   [Kevin, Jack]   [Kevin, Antoine, Iv]
2   [Antoine, Mary, Johanne, Iv]    [Antoine, Mary, Ali]
3   [Ali]   [Mary, Jack]

I want to get a new dataframe that will count all comments all people made.我想获得一个新的数据框,它将计算所有人发表的所有评论。 Something like:就像是:

Kevin凯文 Jack杰克 Antoine安托万 Mary玛丽 Johanne约翰妮 Iv Ali阿里
Kevin凯文 1 1 0 0 1 1 0 0 0 0 1 1 0 0
Jack杰克 1 1 0 0 1 1 0 0 0 0 1 1 0 0
Antoine安托万 0 0 0 0 1 1 1 1 0 0 0 0 1 1
Mary玛丽 0 0 0 0 1 1 1 1 0 0 0 0 1 1
Johanne约翰妮 0 0 0 0 1 1 1 1 0 0 0 0 1 1
Iv 0 0 0 0 1 1 1 1 0 0 0 0 1 1
Ali阿里 0 0 1 1 0 0 1 1 0 0 0 0 0 0

This dataframe might be too small for it to make sense, but my original dataframe is 100k rows and there will be numbers higher than 0 and 1.这个数据框可能太小而无法理解,但我的原始数据框是 100k 行,并且会有高于 0 和 1 的数字。

I've looked at various options using pivot_table and several variations of group by but I can't seem to figure this out.我已经查看了使用 pivot_table 和 group by 的几种变体的各种选项,但我似乎无法弄清楚这一点。

df.pivot_table(index = 'names', columns= 'commented', aggfunc= 'count')

df.groupby('names').commented.apply(list).reset_index()
df.explode('names')['commented'].value_counts()

df.set_index('names').apply(pd.Series.explode).reset_index()

Almost all solutions I tried give me the error: TypeError: unhashable type: 'list'我尝试过的几乎所有解决方案都给我错误: TypeError: unhashable type: 'list'

You can try explode the list of strings to rows then use pandas.crosstab您可以尝试将字符串列表分解为行,然后使用pandas.crosstab

df = (df.explode(df.columns.tolist())
      .apply(lambda col: col.str.split(', '))
      .explode('names')
      .explode('commented'))

out = pd.crosstab(df['names'], df['commented'])
print(df)

     names commented
1    Kevin     Kevin
1    Kevin   Antoine
1    Kevin        Iv
1     Jack     Kevin
1     Jack   Antoine
1     Jack        Iv
2  Antoine   Antoine
2  Antoine      Mary
2  Antoine       Ali
2     Mary   Antoine
2     Mary      Mary
2     Mary       Ali
2  Johanne   Antoine
2  Johanne      Mary
2  Johanne       Ali
2       Iv   Antoine
2       Iv      Mary
2       Iv       Ali
3      Ali      Mary
3      Ali      Jack

print(out)

commented  Ali  Antoine  Iv  Jack  Kevin  Mary
names
Ali          0        0   0     1      0     1
Antoine      1        1   0     0      0     1
Iv           1        1   0     0      0     1
Jack         0        1   1     0      1     0
Johanne      1        1   0     0      0     1
Kevin        0        1   1     0      1     0
Mary         1        1   0     0      0     1

In your sample input, each element in the names and commented columns is an array with only 1 element (a string).在您的示例输入中, namescommented列中的每个元素都是一个只有 1 个元素(字符串)的数组。 Not sure if that is the case with your real data.不确定您的真实数据是否如此。

You can split each string by the comma, and then explode and pivot the dataframe:您可以用逗号分割每个字符串,然后分解和旋转数据框:

split = lambda x: x[0].split(", ")
(
    df.assign(
        names=df["names"].apply(split),
        commented=df["commented"].apply(split),
        dummy=1
    )
    .explode("names")
    .explode("commented")
    .pivot_table(index="names", columns="commented", values="dummy", aggfunc="count", fill_value=0)
)

Here is another way using str.get_dummies()这是使用str.get_dummies()的另一种方法

(df.assign(names = df['names'].str[0].str.split(', '))
.explode('names')
.set_index('names')
.squeeze()
.str[0]
.str.get_dummies(sep=', '))

Output:输出:

         Ali  Antoine  Iv  Jack  Kevin  Mary
names                                       
Kevin      0        1   1     0      1     0
Jack       0        1   1     0      1     0
Antoine    1        1   0     0      0     1
Mary       1        1   0     0      0     1
Johanne    1        1   0     0      0     1
Iv         1        1   0     0      0     1
Ali        0        0   0     1      0     1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM