我想将所有的string值转换Pandas DataFrame成float,我可以定义一个简短的函数来做到这一点,但它不是Pythonic的方法.我的DataFrame看起来像这样:
>>> df = pd.DataFrame(np.array([['1', '2', '3'], ['4', '5', '6']]))
>>> df
0 1 2
0 1 2 3
1 4 5 6
>>> df.dtypes
0 object
1 object
2 object
dtype: object
>>> type(df[0][0])
<type 'str'>
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有一些内置函数Pandas DataFrame将所有string值转换为float.如果你知道Pandas doc上的内置函数,请发布链接.
我有一个Pandas DataFrame它有11列,但我想将DataFrame从第1列而不是第0列拆分到第10列.我可以通过更复杂的方法实现它,而不是Pandas自己提供的方法.如何使用DataFrame支持的方法来完成Pandas?
数据是595行乘11列,我想得到:
>>> import numpy as np
>>> import pandas as pd
>>> train_data = pd.DataFrame(my_data, columns=my_columns)
>>> train_data
stockid prich_m1 prich_m3 prich_m6 \
1 000002.SZ 1.55755700445 0.861009772647 5.42726384781
2 000009.SZ 3.00223270244e-07 4.8010096027 4.46164511978
.. ... ... ... ...
.. ... ... ... ...
594 603699.SH 0.0491892903353 0.934596516371 0.0196757161342
595 603993.SH 0.83105321611 0.771692272102 2.02816558693
rsi mkt_cap held_by_ins found_own \
1 0.650879566982 153108876954.0 42.6353598479 14.9550575226
2 0.462085308057 19492802690.5 25.8866394448 5.31468116104
.. ... …Run Code Online (Sandbox Code Playgroud) 我是新来的Spark,我当前的版本是1.3.1。我想用 实现逻辑回归PySpark,所以,我从Spark Python MLlib找到了这个例子
from pyspark.mllib.classification import LogisticRegressionWithLBFGS
from pyspark.mllib.regression import LabeledPoint
from numpy import array
# Load and parse the data
def parsePoint(line):
values = [float(x) for x in line.split(' ')]
return LabeledPoint(values[0], values[1:])
data = sc.textFile("data/mllib/sample_svm_data.txt")
parsedData = data.map(parsePoint)
# Build the model
model = LogisticRegressionWithLBFGS.train(parsedData)
# Evaluating the model on training data
labelsAndPreds = parsedData.map(lambda p: (p.label, model.predict(p.features)))
trainErr = labelsAndPreds.filter(lambda (v, p): v != p).count() / float(parsedData.count())
print("Training Error = " …Run Code Online (Sandbox Code Playgroud) 我Scrapy用来抓取数据。
在JS浏览器的控制台上,我键入$x('//div[@class="summary"]//div[contains(@class, "tags")]')以获取所需的内容,但需要过滤数据。
下图是$x('//div[@class="summary"]//div[contains(@class, "tags")]')命令结果。
我应该如何编写xpath命令以获取绿色框中的数据?我尝试过$x('//div[@class="summary"]//div[contains(@class, "tags")]//a[contains(@class, "post-tag")]'),但这不是我想要的吗?
谢谢!
我想从集合中导出前 1-100000 条记录。
我知道我可以输入> db.mycollection.find({}).limit(1000000),但它只是获取数据,需要将数据导出为 json 文件。
样本数据
> db.minibars.find({}).limit(3)
{ "_id" : ObjectId("575fd03f6a6253c0b30dd54c"), "Symbol" : "MSFT", "Timestamp" : "2009-08-24 09:30", "Day" : 24, "Open" : 24.41, "High" : 24.42, "Low" : 24.31, "Close" : 24.31, "Volume" : 683713 }
{ "_id" : ObjectId("575fd0406a6253c0b30dd54d"), "Symbol" : "MSFT", "Timestamp" : "2009-08-24 09:31", "Day" : 24, "Open" : 24.32, "High" : 24.33, "Low" : 24.28, "Close" : 24.3, "Volume" : 207651 }
{ "_id" : ObjectId("575fd0406a6253c0b30dd54e"), "Symbol" : "MSFT", "Timestamp" …Run Code Online (Sandbox Code Playgroud) 我有一个命名函数Keys()来获取地图的所有键,这里是代码:
func main() {
m2 := map[int]interface{}{
2:"string",
3:"int",
}
fmt.Println(Keys(m2))
}
func Keys(m map[interface{}]interface{}) (keys []interface{}) {
for k := range m {
keys = append(keys, k)
}
return keys
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了
cannot use m2 (type map[int]interface {}) as type map[interface {}]interface {} in argument to Keys
Run Code Online (Sandbox Code Playgroud)
Go支持泛型,我该如何修复代码?
假设我的模块是myclass.py,这里是代码:
#!/usr/bin/env python
# coding=utf-8
class A(object):
b = B()
def __init__(self):
pass
class B(object):
pass
Run Code Online (Sandbox Code Playgroud)
并导入它
In [1]: import myclass
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-e891426834ac> in <module>()
----> 1 import myclass
/home/python/myclass.py in <module>()
2 # coding=utf-8
3
----> 4 class A(object):
5 b = B()
6 def __init__(self):
/home/python/myclass.py in A()
3
4 class A(object):
----> 5 b = B()
6 def __init__(self):
7 pass
NameError: name 'B' is not defined
Run Code Online (Sandbox Code Playgroud)
我知道如果我在类A上面定义类B,那就没问题,没有错误.但是,我不想这样做,有没有其他方法可以解决这个问题.我知道在C中有功能声明.谢谢!
拉取镜像后,例如ubuntu,如何在本地磁盘上找到镜像文件。
$ docker pull ubuntu
Run Code Online (Sandbox Code Playgroud)
如果我将镜像文件复制到另一台机器上,该镜像还能工作吗?
操作系统:macOS
我正在使用ShuffleSplit来重新排列数据,但我发现存在错误
TypeError Traceback (most recent call last)
<ipython-input-36-192f7c286a58> in <module>()
1 # Fit the training data to the model using grid search
----> 2 reg = fit_model(X_train, y_train)
3
4 # Produce the value for 'max_depth'
5 print "Parameter 'max_depth' is {} for the optimal model.".format(reg.get_params()['max_depth'])
<ipython-input-34-18b2799e585c> in fit_model(X, y)
32
33 # Fit the grid search object to the data to compute the optimal model
---> 34 grid = grid.fit(X, y)
35
36 # Return the optimal …Run Code Online (Sandbox Code Playgroud) 如何获取表的列名、数据类型、大小和注释?
我试过
SELECT all_tab.column_name, all_tab.data_type, all_tab.data_length, col_com.COMMENTS
FROM all_tab_columns all_tab
JOIN user_col_comments col_com ON all_tab.TABLE_NAME = col_com.TABLE_NAME
WHERE all_tab.TABLE_NAME='MY_TABLE'
Run Code Online (Sandbox Code Playgroud)
但它没有用。
python ×5
pandas ×2
apache-spark ×1
css ×1
docker ×1
generics ×1
go ×1
go-reflect ×1
grid-search ×1
href ×1
javascript ×1
mongodb ×1
oracle ×1
pyspark ×1
scikit-learn ×1
scrapy ×1
sql ×1
sqldatatypes ×1
tablecolumn ×1
xpath ×1