如何np.nan在DataFrame中随机插入?假设我想在DataFrame中使用10%的空值.
我的数据如下:
df = pd.DataFrame(np.random.randn(5, 3),
index=['a', 'b', 'c', 'd', 'e'],
columns=['one', 'two', 'three'])
one two three
a 0.695132 1.044791 -1.059536
b -1.075105 0.825776 1.899795
c -0.678980 0.051959 -0.691405
d -0.182928 1.455268 -1.032353
e 0.205094 0.714192 -0.938242
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法来插入空值?
我想使用来自sklearn的StandardScaler的几个方法.是否可以在我的集合的某些列/功能上使用这些方法,而不是将它们应用于整个集合.
例如,该集合是sklearn:
data = pd.DataFrame({'Name' : [3, 4,6], 'Age' : [18, 92,98], 'Weight' : [68, 59,49]})
Age Name Weight
0 18 3 68
1 92 4 59
2 98 6 49
col_names = ['Name', 'Age', 'Weight']
features = data[col_names]
Run Code Online (Sandbox Code Playgroud)
我适合并改造了 StandardScaler
scaler = StandardScaler().fit(features.values)
features = scaler.transform(features.values)
scaled_features = pd.DataFrame(features, columns = col_names)
Name Age Weight
0 -1.069045 -1.411004 1.202703
1 -0.267261 0.623041 0.042954
2 1.336306 0.787964 -1.245657
Run Code Online (Sandbox Code Playgroud)
但当然名称不是浮点数而是字符串,我不想将它们标准化.我怎样才能应用data和data功能只在列fit和transform?
我试图将一个简单的numpy数组转换为pandas数据帧.
x是我的数组,nam是列名列表.
x = np.array([2,3,1,0])
nam = ['col1', 'col2', 'col3', 'col4']
Run Code Online (Sandbox Code Playgroud)
我pd.DataFrame用来转换x
y = pd.DataFrame(x, columns=nam)
Run Code Online (Sandbox Code Playgroud)
但是我有这个错误消息:
ValueError:传递值的形状为(1,4),索引暗示(4,4)
我试图调整索引参数但我找不到解决方案.
我希望我的数据框看起来像这样:
col1 col2 col3 col4
2 3 1 0
Run Code Online (Sandbox Code Playgroud) {"57ecf565817bc3932d8de349": {
"prices": {
"2017-07-01": {
"summer": -1,
"winter": -1,
"xmas": -1},
"2017-08-05": {
"summer": -1,
"winter": -1,
"xmas": -1}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何编写每个键都不同的 JSON 模式(此处:2017-07-01、2017-08-05)所以我不能items像在这个例子中那样使用http://json-schema.org/example1.html #definitions(产品架构集)。
这是我的架构:
{
"type": "object",
"properties": {
"57ecf565817bc3932d8de349": {
"type": "object",
"properties": {
"prices": {
"type": "object",
"properties": {
"2017-07-01": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
},
"xmas": {
"type": "integer"
}
},
"required": [
"summer",
"winter",
"xmas"
]
},
"2017-08-05": { …Run Code Online (Sandbox Code Playgroud) pandas ×3
python ×3
numpy ×2
arrays ×1
data-science ×1
dataframe ×1
json ×1
jsonschema ×1
missing-data ×1
scale ×1
scikit-learn ×1