小编Pie*_* S.的帖子

dict 的 dict 上的 Mypy 错误:“对象”类型的值不可索引

我在 python 上有以下字典:

dictionary = {
    'key1': 1,
    'sub_dict': {'key2': 0},
}
Run Code Online (Sandbox Code Playgroud)

当我在以下行上运行 mypy 时:

print(dictionary['sub_dict']['key2'])
Run Code Online (Sandbox Code Playgroud)

它引发了错误 Value of type "object" is not indexable

python dictionary mypy

8
推荐指数
2
解决办法
7905
查看次数

将标量的字典转换为 Pandas DataFrame

在 Python3 下,我有一个格式如下的字典:

my_dict = {'col1': 1.0, 'col2':2.0, 'col3': 3.0}
Run Code Online (Sandbox Code Playgroud)

我想使用 dict 键作为列将其转换为 Pandas DataFrame:

      col1  col2  col3
0     1.0   2.0   3.0
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试以下命令时,出现 ValueError:

df = pd.DataFrame(my_dict)

ValueError: If using all scalar values, you must pass an index
Run Code Online (Sandbox Code Playgroud)

dictionary python-3.x pandas

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

如何在 Python 中删除异常值?

我想从我的数据集“train”中删除异常值,为此我决定使用 z-score 或 IQR。

我在 Microsoft Python Client for SQL Server 上运行 Jupyter notebook。

我试过 z-score:

from scipy import stats
train[(np.abs(stats.zscore(train)) < 3).all(axis=1)]
Run Code Online (Sandbox Code Playgroud)

对于 IQR:

Q1 = train.quantile(0.02)
Q3 = train.quantile(0.98)
IQR = Q3 - Q1
train = train[~((train < (Q1 - 1.5 * IQR)) |(train > (Q3 + 1.5 * 
IQR))).any(axis=1)]
Run Code Online (Sandbox Code Playgroud)

...返回...

对于 z 分数:

类型错误:不支持 / 的操作数类型:'str' 和 'int'

对于 IQR:

类型错误:无法排序的类型:str() < float()

我的火车数据集如下所示:

# Number of each type of column
print('Training data shape: ', train.shape)
train.dtypes.value_counts() …
Run Code Online (Sandbox Code Playgroud)

python numpy scipy pandas

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

标签 统计

dictionary ×2

pandas ×2

python ×2

mypy ×1

numpy ×1

python-3.x ×1

scipy ×1