我在 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
在 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) 我想从我的数据集“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)