小编DIG*_*SUM的帖子

Python Pandas:仅保留包含第一个项目的数据帧行

我有这个:

    Date value
0   1975     a
21  1975     b
1   1976     b
22  1976     c
3   1977     a
2   1977     b
4   1978     c
25  1978     d
5   1979     e
26  1979     f
6   1980     a
27  1980     f
Run Code Online (Sandbox Code Playgroud)

我很难找到一种方法来只保留包含第一次出现'value'的行.我想删除重复的'值',保持行的最低'日期'.最终结果应该是:

    Date value
0   1975     a
21  1975     b
22  1976     c
25  1978     d
5   1979     e
26  1979     f
Run Code Online (Sandbox Code Playgroud)

python pandas

3
推荐指数
2
解决办法
4520
查看次数

熊猫数据帧频率

我有这个数据帧:

source target
0     ape    dog
1     ape   hous
2     dog   hous
3    hors    dog
4    hors    ape
5     dog    ape
6     ape   bird
7     ape   hous
8    bird   hous
9    bird   fist
10   bird    ape
11   fist    ape
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下代码生成频率计数:

df_count =df.groupby(['source', 'target']).size().reset_index().sort_values(0, ascending=False)
df_count.columns = ['source', 'target', 'weight']
Run Code Online (Sandbox Code Playgroud)

我得到下面的结果.

source target  weight
2     ape   hous       2
0     ape   bird       1
1     ape    dog       1
3    bird    ape       1
4    bird   fist       1
5    bird   hous       1
6     dog    ape …
Run Code Online (Sandbox Code Playgroud)

python pandas

3
推荐指数
1
解决办法
73
查看次数

Python中的“ display_formatter”属性错误

我正在研究一个主题建模脚本,该脚本使用gensim包基于文档集合创建模型。

当准备使用pyLDAvis软件包可视化模型时,遇到了以下错误:

import pyLDAvis
pyLDAvis.enable_notebook()

Traceback (most recent call last):
  File "/dev2.py", line 2, in <module>
    pyLDAvis.enable_notebook()
  File "/Users/username/Library/Python/3.6/lib/python/site-packages/pyLDAvis/_display.py", line 311, in enable_notebook
    formatter = ip.display_formatter.formatters['text/html']
AttributeError: 'NoneType' object has no attribute 'display_formatter'
Run Code Online (Sandbox Code Playgroud)

python error-handling lda

1
推荐指数
1
解决办法
1899
查看次数

绕过 IncompleteRead 异常

我正在使用 Tweepy 在 Python3 中编写 Twitter 流侦听器。流一段时间后我收到此错误:

urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
Run Code Online (Sandbox Code Playgroud)

我怎么能绕过这个,重新连接并继续?

我已经做好了:

from requests.packages.urllib3.exceptions import ReadTimeoutError, IncompleteRead
Run Code Online (Sandbox Code Playgroud)

和:

while True:
    try:
        twitter_stream.filter(track=keywordlist, follow=userlist)

    except IncompleteRead:
        continue
Run Code Online (Sandbox Code Playgroud)

但仍然收到错误。

python urllib3 tweepy

1
推荐指数
1
解决办法
2138
查看次数

在Python中使用AND运算符匹配行

我编写了下面的代码,用于查找infile中与关键字文件中的任何关键字匹配的行.问题是,我想只得到那些包含所有关键字的infile行.似乎比我想象的更难,但我是初学者,所以我想我只是错过了一些明显的东西.然而,正则表达式似乎没有一个简单的'和'运算符.

import re
infile = open('path/#input.txt', 'r')
outfile = open('path/#output.txt', 'w')

# Read a textfile containing keywords to find
# (and strip the newline character '\n')
keywords = [line.strip() for line in open('path/#keywords.txt')]

# Compile keywords into a regex pattern 
pattern = re.compile('|'.join(keywords))

# See which lines in the infile match any of the keywords
# and write those lines to the outfile
for line in infile:
    if pattern.search(line):
        outfile.write(line)
Run Code Online (Sandbox Code Playgroud)

python regex

0
推荐指数
1
解决办法
80
查看次数

用tidytext删除停用词

使用tidytext,我有这个代码:

data(stop_words)
tidy_documents <- tidy_documents %>%
      anti_join(stop_words)
Run Code Online (Sandbox Code Playgroud)

我希望它使用包中内置的停用词将名为tidy_documents的数据帧写入同名的数据帧中,但如果它们在stop_words中,则删除单词.

我收到此错误:

错误:没有常见变量.请指定by参数.追溯:

1. tidy_documents %>% anti_join(stop_words)
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3. eval(quote(`_fseq`(`_lhs`)), env, env)
4. eval(expr, envir, enclos)
5. `_fseq`(`_lhs`)
6. freduce(value, `_function_list`)
7. withVisible(function_list[[k]](value))
8. function_list[[k]](value)
9. anti_join(., stop_words)
10. anti_join.tbl_df(., stop_words)
11. common_by(by, x, y)
12. stop("No common variables. Please specify `by` param.", call. = FALSE)
Run Code Online (Sandbox Code Playgroud)

r dplyr tidyverse tidytext

0
推荐指数
2
解决办法
7175
查看次数

击中副本时突破while循环

我有这个代码:

steps = [['A', 'B', 'C', 'C', 'C'], ['D', 'E', 'F', 'F', 'F']]

for step in steps:

    while True:
        last_item = ""

        for item in step:
            if item != last_item: 
                print(item)
                last_item = item
            else:
                break
Run Code Online (Sandbox Code Playgroud)

所需的结果是循环打印A,然后是B,然后是C,但是当击中第一个副本C时,它应该继续打印D,然后是E,然后是F,然后在击中第一个副本F时停止.

这是一个在Web抓取工作中使用的循环的最小可重现示例,因此涉及set(steps)对示例执行或其他操作的解决方案steps将无法解决此问题.我的问题必须与循环的体系结构.

python

0
推荐指数
1
解决办法
92
查看次数

标签 统计

python ×6

pandas ×2

dplyr ×1

error-handling ×1

lda ×1

r ×1

regex ×1

tidytext ×1

tidyverse ×1

tweepy ×1

urllib3 ×1