繁体   English   中英

将Pandas数据框中字符串出现的次数附加到另一列

[英]Append number of times a string occurs in Pandas dataframe to another column

我想在这个数据帧上创建一个额外的列:

Index                  Value
0                22,88,22,24
1                      24,24
2                      22,24
3    11,22,24,12,24,24,22,24
4                         22

因此,值发生的次数存储在新列中:

Index                  Value     22 Count
0                22,88,22,24            2
1                      24,24            1
2                      22,24            1
3    11,22,24,12,24,24,22,24            2
4                         22            1

我想在value列中为许多不同的值重复此过程。

我最小的Python知识告诉我类似的东西:

df['22 Count'] = df['Value'].count('22')

我试过这个和其他几个版本但我必须遗漏一些东西。

如果只想计算一个值,请使用str.count

df['22 Count'] = df['Value'].str.count('22')
print (df)
                         Value  22 Count
Index                                   
0                  22,88,22,24         2
1                        24,24         0
2                        22,24         1
3      11,22,24,12,24,24,22,24         2
4                           22         1

对于所有列数需要:

from collections import Counter

df1 = df['Value'].apply(lambda x: pd.Series(Counter(x.split(','))), 1).fillna(0).astype(int)

要么:

df1 = pd.DataFrame([Counter(x.split(',')) for x in df['Value']]).fillna(0).astype(int)

要么:

from sklearn.feature_extraction.text import CountVectorizer

countvec = CountVectorizer()
counts = countvec.fit_transform(df['Value'].str.replace(',', ' '))
df1 = pd.DataFrame(counts.toarray(), columns=countvec.get_feature_names())

print (df1)
   11  12  22  24  88
0   0   0   2   1   1
1   0   0   0   2   0
2   0   0   1   1   0
3   1   1   2   4   0
4   0   0   1   0   0

最后如果需要添加到原始:

df = df.join(df1.add_suffix(' Count'))
print (df)
                         Value  11 Count  12 Count  22 Count  24 Count  \
Index                                                                    
0                  22,88,22,24         0         0         2         1   
1                        24,24         0         0         0         2   
2                        22,24         0         0         1         1   
3      11,22,24,12,24,24,22,24         1         1         2         4   
4                           22         0         0         1         0   

       88 Count  
Index            
0             1  
1             0  
2             0  
3             0  
4             0  

孤立的计数

你很亲密 但是您的语法会尝试将系列视为列表。 相反,您可以转换为list 使用count方法:

from operator import methodcaller

df['22_Count'] = df['Value'].str.split(',').apply(methodcaller('count', '22'))

print(df)

   Index                    Value  22_Count
0      0              22,88,22,24         2
1      1                    24,24         0
2      2                    22,24         1
3      3  11,22,24,12,24,24,22,24         2
4      4                       22         1

多重计数

使用@jezrael显示的方法。

暂无
暂无

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

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