python - 使用 python 和 pandas 按数据框分组

标签 python pandas dataframe numpy keyword

假设我有这样的 df

<表类="s-表"> <头> 身份证 name_x st 字符串 <正文> 1 xx 我们 不认识浣熊酋长影响了他的晋升前景 2 xy 我们1 立交桥下了高速公路,进入了一个 secret 的世界 3 xz 我们 他 100% 和她一起禁食,直到他明白那意味着他不能进食 4 许 我们2 在其他随机词前面的随机词创建一个随机句子 5 习 我们1 拿起笔开始

st 列使用 python 和 pandas 我想计算 name_x 值,然后从字符串中提取前 3 个关键词。

例如像这样:

<表类="s-表"> <头> st name_x_count top1_word top2_word top3_word <正文> 我们 2 单词1 词2 单词3 我们1 2 单词1 词2 单词3 我们2 1 单词1 词2 单词3

有什么办法可以解决这个任务吗?

最佳答案

我会首先使用 groupby() 来连接您显示的字符串,然后使用集合 Counter,然后使用 most_common。最后将其分配回数据框。我正在使用 x.lower(),否则“他”和“他”将被视为不同的词(但如果有意,您可以随时将其删除):

output = df.groupby('st').agg(
    name_x_count = pd.NamedAgg('name_x','count'),
    string = pd.NamedAgg('string',' '.join))

分组后,我们使用 collections.Counter() 创建列:

output[['top1_word','top2_word','top3_word']] = output['string'].map(lambda x: [x[0] for x in collections.Counter(x.lower().split()).most_common(3)])
output = output.drop(columns='string')

输出:

     name_x_count top1_word top2_word top3_word
st                                             
us              2        he      with       was
us1             2       the       and  overpass
us2             1    random     words        in

关于python - 使用 python 和 pandas 按数据框分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74385812/

相关文章:

python - Pandas Dataframe - 如何检查 A 列中的字符串值是否在 B 列中的字符串项列表中可用

python - 从 pandas 系列中选择列

Python,DataFrame - 查找一行中的某个值

python - Pandas 数据帧 : area plot stacked ='false' not working

python - 有没有办法根据另一列的值是否在 python 中的数字范围内来填充列?

python - Bool对象不支持item赋值

python - 从字典列表中创建 Django 模型

python - SQLAlchemy 关联困惑

python - 有条件地赋值(有多个条件)

python - 如何在没有 pandas 中的 to_datetime 函数的情况下格式化列中的日期时间值?