小编Adr*_*ian的帖子

Python:BeautifulSoup UnboundLocalError

我试图从.txt格式的某些文档中删除HTML标记.但是,据我所知,bs4似乎有错误.我得到的错误如下:

 Traceback (most recent call last):
  File "E:/Google Drive1/Thesis stuff/Python/database/get_missing_10ks.py", line 13, in <module>
    text = BeautifulSoup(file_read, "html.parser")
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\site-packages\bs4\__init__.py", line 282, in __init__
    self._feed()
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\site-packages\bs4\__init__.py", line 343, in _feed
    self.builder.feed(self.markup)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\site-packages\bs4\builder\_htmlparser.py", line 247, in feed
    parser.feed(markup)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\html\parser.py", line 111, in feed
    self.goahead(0)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\html\parser.py", line 179, in goahead
    k = self.parse_html_declaration(i)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\html\parser.py", line 264, in parse_html_declaration
    return self.parse_marked_section(i)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\_markupbase.py", line 160, in parse_marked_section
    if not match:
UnboundLocalError: …
Run Code Online (Sandbox Code Playgroud)

html python parsing beautifulsoup text-files

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

熊猫:获取2个数据框列之间的最小值

我有2列,我希望第3列是它们之间的最小值。我的数据如下所示:

   A  B
0  2  1
1  2  1
2  2  4
3  2  4
4  3  5
5  3  5
6  3  6
7  3  6
Run Code Online (Sandbox Code Playgroud)

我想通过以下方式获取列C:

   A  B   C
0  2  1   1
1  2  1   1
2  2  4   2
3  2  4   2
4  3  5   3
5  3  5   3
6  3  6   3
7  3  6   3
Run Code Online (Sandbox Code Playgroud)

一些帮助代码:

df = pd.DataFrame({'A': [2, 2, 2, 2, 3, 3, 3, 3],
                   'B': [1, 1, 4, 4, …
Run Code Online (Sandbox Code Playgroud)

python min dataframe python-3.x pandas

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

Pandas:计算一列每两行的平均值并将其放入新列中

我想对一列求平均值,但我希望将平均值放入带有 pandas 的新列中。

我想从这种格式开始:

values
10
5
8
7
2
5
6
7
Run Code Online (Sandbox Code Playgroud)

对于这种格式:

values  average
10  nan
5   7.5
8   6.5
7   7.5
2   4.5
5   3.5
6   5.5
7   6.5
Run Code Online (Sandbox Code Playgroud)

这里有一个类似的解决方案:在 pandas dataframe 中平均每两个连续索引值(每 2 分钟),但我想保持相同的行数。

python average dataframe pandas

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

Pandas-Dataframe:如何计算变量在 1 分钟内重复的次数

我有以下数据帧片段:

Full dataframe:                   ip      time      cik  crawler
ts                                                              
2019-03-11 00:00:01   71.155.177.ide  00:00:01  1262327      0.0
2019-03-11 00:00:02   71.155.177.ide  00:00:02  1262329      0.0
2019-03-11 00:00:05   69.243.218.cah  00:00:05   751200      0.0
2019-03-11 00:00:08  172.173.121.efb  00:00:08   881890      0.0
2019-03-11 00:00:09   216.254.60.idd  00:00:09  1219169      0.0
2019-03-11 00:00:09    64.18.197.gjc  00:00:09  1261705      0.0
2019-03-11 00:00:09    64.18.197.gjc  00:00:09  1261734      0.0
2019-03-11 00:00:10    64.18.197.gjc  00:00:10  1263094      0.0
2019-03-11 00:00:10    64.18.197.gjc  00:00:10  1264242      0.0
2019-03-11 00:00:10    64.18.197.gjc  00:00:10  1264242      0.0
Run Code Online (Sandbox Code Playgroud)

我想按 IP 分组,然后使用一些函数来计数:

1) 1 分钟内每个 IP 有多少唯一 CIK

2) 1 分钟内每个 …

datetime dataframe python-3.x pandas pandas-groupby

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

Pandas:计算数据框中所有行和特定行之间的差异

您好,我有以下格式的数据:

   A  B
0  2  1
1  2  1
2  2  4
3  2  4
4  3  5
5  3  5
6  3  6
7  3  6
Run Code Online (Sandbox Code Playgroud)

我想计算索引 0 和所有其他索引之间的绝对差之和。这意味着我将计算每一列的差异。取绝对值并对这些值求和。我想创建一个“C”列,如下所示:

    A   B   C
0   2   1   0
1   2   1   0
2   2   4   3
3   2   4   3
4   3   5   5
5   3   5   5
6   3   6   6
7   3   6   6
Run Code Online (Sandbox Code Playgroud)

例如,在索引 7 处,计算按以下方式完成:

索引 7[C] = ABS(索引 0[A]- 索引 7[A]) + ABS(索引 0[B] - 索引 …

python dataframe python-3.x pandas difference

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

使用Python的选择性文本

我是python的初学者,我将它用于我的硕士论文,所以我不知道那么多.我有一堆年度报告(采用txt格式)文件,我想选择"ITEM1"之间的所有文本.和"ITEM2.".我正在使用重新包装.我的问题是,有时候,在那些10ks中,有一个名为"ITEM1A"的部分.我希望代码能够识别出这个并停在"ITEM1A".并在输出中输入"ITEM1"之间的文本.和"ITEM1A.".在我附加到这篇文章的代码中,我试图让它停在"ITEM1A.",但它没有,它继续进一步因为"ITEM1A".在文件中多次出现.我会理想的是让它停在它看到的第一个.代码如下:

import os
import re

#path to where 10k are
saved_path = "C:/Users/Adrian PC/Desktop/Thesis stuff/10k abbot/python/Multiple 10k/saved files/"

#path to where to save the txt with the selected text between ITEM 1 and ITEM 2
selected_path = "C:/Users/Adrian PC/Desktop/Thesis stuff/10k abbot/python/Multiple 10k/10k_select/"

#get a list of all the items in that specific folder and put it in a variable
list_txt = os.listdir(saved_path)


for text in list_txt:
    file_path = saved_path+text
    file = open(file_path,"r+", encoding="utf-8")
    file_read = file.read()
    # looking between ITEM …
Run Code Online (Sandbox Code Playgroud)

python text selection

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

python中2个列表的线性回归

嗨,我有 2 个数字列表,我想从常规线性回归中获得 R^2。我认为这个问题已经发布了很多,但我在某处找不到这个问题。

我的清单:

my_y = [2,5,6,10]
my_x = [19,23,22,30]
Run Code Online (Sandbox Code Playgroud)

我试图将其更改为 numpy 数组,然后使用 sklearn 回归并获得我需要的内容,但我没有成功。我使用了以下代码:

from sklearn.linear_model import LinearRegression
import numpy as np

my_y = np.array([2,5,6,10]).reshape(1, -1)
my_x = np.array([19,23,22,30]).reshape(1,-1)

lm = LinearRegression()
result = lm.score(my_x, my_y)
print(result)
Run Code Online (Sandbox Code Playgroud)

有没有人有一种快速的方法可以通过在这两个变量之间进行线性回归来获得 R^2?

我从这个回归中得到的预期输出是:R^2=0.930241

python numpy list linear-regression python-3.x

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

将数据框的切片添加到新列中的另一个数据框

我有2个数据框。一个是空的,另一个是很多行。我想将数据框与值分组,然后切片每组的前3行,并将其添加到空数据框。我希望将每3个新行放入一个新列中。

我已经尝试过,concat,加入,追加..但我不知道如何...

到目前为止,我的代码:

df = pd.Dataframe()
df2 = pd.DataFrame({'C': [20, 20, 20, 20, 10, 10, 10, 30, 30, 30],
                   'D': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})

df_dictionary = df2.groupby("C")

for key, df_values in df_dictionary:
    df_values = df_values.head(3)
    df = pd.concat(df, df_values["D"], axis=1)
    print(df)
Run Code Online (Sandbox Code Playgroud)

结果看起来像是空白数据框:

index   col 1   col 2   col 3
0   1   5   8
1   2   6   9
2   3   7   10
Run Code Online (Sandbox Code Playgroud)

我想将每个组的D列中的前3个值添加到空数据框中,并每次将它们放在新列中。

有人有建议吗?

python dataframe python-3.x pandas

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