小编Sno*_*ing的帖子

忽略字典 `count_if=1` 的 Pandas 聚合。

我想使用sum并获取Pandas DataFrame 来聚合 Pandas DataFrame NaN,如果一组的所有值都是NaN。这适用于.agg('sum', min_count=1)min_count在使用聚合字典时被忽略的情况。

我在这里缺少什么以及如何解决它?

例子:

import pandas as pd
import numpy as np
d = {'l':  ['a', 'b', 'a', 'b', 'c', 'c'],
     'v': [-1, 1, np.nan, 1, np.nan, np.nan],
     'w': [-1, 1, np.nan, 1, np.nan, np.nan]}
df = pd.DataFrame(d)
Run Code Online (Sandbox Code Playgroud)

sum将返回0.0全部的组NaN,但我们可以通过使用参数来解决这个问题min_count=1

print(df.groupby('l')['v'].agg('sum'))
> l
> a   -1.0
> b    2.0
> c    0.0
> Name: v, dtype: float64

print(df.groupby('l')['v'].agg('sum', min_count=1))
> …
Run Code Online (Sandbox Code Playgroud)

python pandas

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

来自事件坐标的 Matplotlib 日期时间

我确信这与其他地方的其他问题重复,但我找不到任何答案......对此感到抱歉,任何链接也将受到赞赏;)

我将 x 轴作为日期时间,并希望获取发生点击的日期时间。相反,我得到了一些我不明白的坐标。如何将这些坐标转换为日期时间 (str)?

考虑以下示例:

import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
import mpldatacursor        # pip install mpldatacursor

df = pd.DataFrame(index=[dt.datetime.now() + dt.timedelta(i) for i in range(20)], 
                  data={'val1': [10/(i+1) for i in range(20)],
                        'val2': [5 * 2**(-i) for i in range(20)]})
fig, ax = plt.subplots()
df.plot(ax=ax)

# mpldatacursor opens a dragable yellow rectangle with the informations
# for that point. The kwargs `x` and `y` seem to be the coords.
def myformatter(**kwargs):
    # For …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

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

精益:定义R理想和R模块的乘积

我正在尝试学习精益,并且试图弄清楚如何I*M = {i*m | i in I, m in M}从理想的I和R模块M 创建新的R模块。

因此,我的尝试是首先定义一个映射ideal_mult,该映射将创建一个新的R模块,然后弄清楚如何为其分配一个好的符号。

import ring_theory.ideals
import algebra.module

universes u v
variables {R : Type u} {M : Type v}
variables [comm_ring R] [add_comm_group M] [module R M]
variables (I: ideal R)

def ideal_mult (I: ideal R) (M: Type v)
        [add_comm_group M] [module R M]: Type v
        :=
    sorry

#check ideal_mult I M
Run Code Online (Sandbox Code Playgroud)

我该如何定义它,以便例如举例说明一个假设(h: I*M = M)

感谢您的帮助!

lean

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

OpenCV:文本处理和噪声消除

我想删除包含文本的图像的背景,使其在白色背景上的文本.

图像样本
在此输入图像描述

到目前为止我已经尝试过获取图像的HSV和上下边界,但我找不到可以去除所有背景效果的上下边界

直到现在使用的代码:

import cv2
import numpy as np


# Take each frame
filename = 'img2.png'

img = cv2.imread(filename, 1)

# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
# Threshold the HSV image to get only blue colors
image_final = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)
cv2.imshow('frame',img)
cv2.imwrite('mask.png',image_final)


cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法,或者我必须结合多个下边界和上边界来达到我的目标?

python opencv opencv3.0

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

标签 统计

python ×3

lean ×1

matplotlib ×1

opencv ×1

opencv3.0 ×1

pandas ×1