小编Bra*_*mon的帖子

设置一个字符串作为pandas DataFrame的索引

给定数据框 df

df = pd.DataFrame([1,2,3,4])
print(df)
   0
0  1
1  2
2  3
3  4
Run Code Online (Sandbox Code Playgroud)

我想将其修改为

print(df)
   0
A  1
A  2
A  3
A  4
Run Code Online (Sandbox Code Playgroud)

python indexing dataframe pandas

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

我无法弄清楚这个简洁的代码

def duplicate_count(s):
    return len([c for c in set(s.lower()) if s.lower().count(c)>1])
Run Code Online (Sandbox Code Playgroud)

我很难理解这段代码是如何工作的.我正在进行代码检查挑战,以返回字符串中包含重复项的元素数量.

例如.Asasd - > 2

我想出了自己的命令,但我无法真正理解这段代码的作用.如果有人能指出我的方向,将非常感谢:)

python python-3.x

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

在python中找到价格动量的有效方法:平均列的最后n个条目

我定义的价格动量是过去n天给定股票动量的平均值.

反过来,动量是一种分类:如果当天的收盘价高于前一天,则每天标记为1;如果价格低于前一天,则标记为-1.

我的库存变化百分比如下:

df['close in percent'] = np.array([0.27772152, 1.05468772, 
                                   0.124156 , -0.39298394, 
                                   0.56415267,  1.67812005])

momentum = df['close in percent'].apply(lambda x: 1 if x > 0 else -1).values
Run Code Online (Sandbox Code Playgroud)

Momentum应该是:[1,1,1,-1,1,1].

因此,如果我发现过去n = 3天的平均动量,我希望我的价格势头为:

Price_momentum = [Nan, Nan, 1, 1/3, 1/3, 1/3]
Run Code Online (Sandbox Code Playgroud)

我设法使用以下代码使其工作,但这非常慢(数据集是5000多行,执行需要10分钟).

for i in range(3,len(df)+1,1):
    data = np.array(momentum[i-3:i])
    df['3_day_momentum'].iloc[i-1]=data.mean()
Run Code Online (Sandbox Code Playgroud)

python numpy pandas

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

使用Python解析BibText引文格式

在python中解析这个结果的最佳方法是什么?我试过正则表达式,但无法让它工作.我正在寻找标题词,作者等作为键.

@article{perry2000epidemiological,
  title={An epidemiological study to establish the prevalence of urinary symptoms and felt need in the community: the Leicestershire MRC Incontinence Study},
  author={Perry, Sarah and Shaw, Christine and Assassa, Philip and Dallosso, Helen and Williams, Kate and Brittain, Katherine R and Mensah, Fiona and Smith, Nigel and Clarke, Michael and Jagger, Carol and others},
  journal={Journal of public health},
  volume={22},
  number={3},
  pages={427--434},
  year={2000},
  publisher={Oxford University Press}
}
Run Code Online (Sandbox Code Playgroud)

python regex bibtex

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

迭代嵌套列表并对每个元素进行操作

我有一个列表列表,我正在尝试删除所有非字母字符。

\n\n

我尝试使用isalpha()

\n\n
data = [\n    ['we', '\\n\\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best'], \n    ['though', '10/3/2011', 'it', 'may', 'not', '\\n\\n', 'make', 'landfall', 'all', 'week', '2 000 \xe2\x82\xac', 'if', 'it', 'follows', 'that', '\xe2\x80\xa2', 'track'],\n    ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '\xe2\x80\xa2', 'floods', '\\n\\n', 'are', 'possible'],\n]\n\nnew_data = ''.join([i for i in data if i.isalpha()])\n
Run Code Online (Sandbox Code Playgroud)\n\n

预期输出:

\n\n
['we will pray and hope for the best', \n 'though it may not make landfall all week if it …
Run Code Online (Sandbox Code Playgroud)

python replace list

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

使用 gpg 格式以编程方式验证校验和

我想以编程方式验证Kafka二进制文件的 sha512 校验和。首先,我下载二进制文件和 sha512 sum 文本文件:

curl -fsSL -O \
    https://ftp.wayne.edu/apache/kafka/2.7.0/kafka_2.13-2.7.0.tgz
curl -fsSL -O \
    https://downloads.apache.org/kafka/2.7.0/kafka_2.13-2.7.0.tgz.sha512
Run Code Online (Sandbox Code Playgroud)

通过手动检查我知道校验和是正确的:

$ cat -ne kafka_2.13-2.7.0.tgz.sha512 
     1  kafka_2.13-2.7.0.tgz: F3DD1FD8 8766D915 0D3D395B 285BFA75 F5B89A83 58223814$
     2                        90C8428E 6E568889 054DDB5F ADA1EB63 613A6441 989151BC$
     3                        7C7D6CDE 16A871C6 674B909C 4EDD4E28$

$ sha512sum kafka_2.13-2.7.0.tgz
f3dd1fd88766d9150d3d395b285bfa75f5b89a835822381490c8428e6e568889054ddb5fada1eb63613a6441989151bc7c7d6cde16a871c6674b909c4edd4e28  kafka_2.13-2.7.0.tgz
Run Code Online (Sandbox Code Playgroud)

但是shasum/sha512sum似乎不喜欢.512进行程序验证的文件格式(0 成功时退出代码,1 失败时退出代码)。

$ cat -ne kafka_2.13-2.7.0.tgz.sha512 
     1  kafka_2.13-2.7.0.tgz: F3DD1FD8 8766D915 0D3D395B 285BFA75 F5B89A83 58223814$
     2                        90C8428E 6E568889 054DDB5F ADA1EB63 613A6441 989151BC$
     3                        7C7D6CDE …
Run Code Online (Sandbox Code Playgroud)

bash checksum sha512 apache-kafka

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

在类方法docstrings中使用字符串格式

我有一个类有几个类似的方法,每个方法都有相似的长文档字符串,但在几个短语/单词方面有所不同.我想构建一个docstring模板,然后将字符串格式应用于它.下面是一个笨拙的实现,其中__doc__s是在类方法之后定义的.

capture_doc = """
%(direc)s normal.

a %(sym)s b."""

class Cls():    
    def a(self):
        pass
    def b(self):
        pass
    a.__doc__ = capture_doc % {'direc' : 'below', 'sym' : '<'}     
    b.__doc__ = capture_doc % {'direc' : 'above', 'sym' : '>'}

c = Cls()    
print(c.a.__doc__)

below normal.

a < b.
Run Code Online (Sandbox Code Playgroud)

问题:是否有Python文档或PEP规定的方法来执行此操作?我想保持基本的东西,我见过使用@Appender 装饰师,但认为这对我的需求有点花哨.

python docstring string-formatting python-3.x

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

为每一行选择不同的列

假设我有以下数组:

>>> a = np.arange(25).reshape((5, 5))
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
Run Code Online (Sandbox Code Playgroud)

现在我想根据以下索引数组为每一行选择不同的列:

>>> i = np.array([0, 1, 2, 1, 0])
Run Code Online (Sandbox Code Playgroud)

此索引数组表示每一行的起始列,并且选择范围应相似,例如 3。因此我想获得以下结果:

>>> ???
array([[ 0,  1,  2],
       [ 6,  7,  8],
       [12, 13, 14],
       [16, 17, 18],
       [20, 21, 22]])
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过

>>> a[np.arange(a.shape[0]), i]
Run Code Online (Sandbox Code Playgroud)

但是多列呢?

python numpy

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