我有一只DataFrame熊猫:
import pandas as pd
inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
print df
Run Code Online (Sandbox Code Playgroud)
输出:
c1 c2
0 10 100
1 11 110
2 12 120
Run Code Online (Sandbox Code Playgroud)
现在我想迭代这个帧的行.对于每一行,我希望能够通过列的名称访问其元素(单元格中的值).例如:
for row in df.rows:
print row['c1'], row['c2']
Run Code Online (Sandbox Code Playgroud)
是否有可能在熊猫中做到这一点?
我发现了类似的问题.但它没有给我我需要的答案.例如,建议使用:
for date, row in df.T.iteritems():
Run Code Online (Sandbox Code Playgroud)
要么
for row in df.iterrows():
Run Code Online (Sandbox Code Playgroud)
但我不明白row对象是什么以及如何使用它.
我正在处理代码,并使用warnings库抛出了很多(对我而言)无用的警告.阅读(/扫描)文档我只发现了一种禁用单个函数警告的方法.但我不想改变这么多的代码.
可能有旗帜python -no-warning foo.py吗?
你会推荐什么?
我可以从决策树中的受过训练的树中提取基础决策规则(或"决策路径")作为文本列表吗?
就像是:
if A>0.4 then if B<0.2 then if C>0.8 then class='X'
谢谢你的帮助.
python machine-learning decision-tree random-forest scikit-learn
如何修改pandas中groupby操作的输出格式,为大数字生成科学记数法.我知道如何在python中进行字符串格式化,但是在这里应用它时我感到很茫然.
df1.groupby('dept')['data1'].sum()
dept
value1 1.192433e+08
value2 1.293066e+08
value3 1.077142e+08
Run Code Online (Sandbox Code Playgroud)
如果我转换为字符串,这会抑制科学记数法,但现在我只是想知道如何字符串格式和添加小数.
sum_sales_dept.astype(str)
Run Code Online (Sandbox Code Playgroud) python floating-point scientific-notation number-formatting pandas
我有一个数据框,其中包含unix时间和价格.我想转换索引列,以便在人类可读日期中显示.因此,例如我在索引列中将"日期"设置为1349633705,但我希望它显示为10/07/2012(或至少10/07/2012 18:15).对于某些上下文,这里是我正在使用的代码以及我已经尝试过的代码:
import json
import urllib2
from datetime import datetime
response = urllib2.urlopen('http://blockchain.info/charts/market-price?&format=json')
data = json.load(response)
df = DataFrame(data['values'])
df.columns = ["date","price"]
#convert dates
df.date = df.date.apply(lambda d: datetime.strptime(d, "%Y-%m-%d"))
df.index = df.date
Run Code Online (Sandbox Code Playgroud)
你可以看到我在date这里使用
哪个不起作用,因为我正在使用整数,而不是字符串.我想我需要使用,1349633705但我不太确定如何将它应用于整个df.date.谢谢.
我正在尝试使用scikit-learn的监督学习方法之一将文本片段分类为一个或多个类别.我尝试的所有算法的预测函数只返回一个匹配.
例如,我有一段文字:
"Theaters in New York compared to those in London"
Run Code Online (Sandbox Code Playgroud)
我已经训练了算法为我提供的每个文本片段选择一个地方.
在上面的例子中,我希望它返回New York和London,但它只返回New York.
是否可以使用scikit-learn返回多个结果?或者甚至以最高概率返回标签?
谢谢你的帮助.
---更新
我尝试过使用,OneVsRestClassifier但我仍然只能在每段文字中找到一个选项.下面是我正在使用的示例代码
y_train = ('New York','London')
train_set = ("new york nyc big apple", "london uk great britain")
vocab = {'new york' :0,'nyc':1,'big apple':2,'london' : 3, 'uk': 4, 'great britain' : 5}
count = CountVectorizer(analyzer=WordNGramAnalyzer(min_n=1, max_n=2),vocabulary=vocab)
test_set = ('nice day in nyc','london town','hello welcome to the big apple. enjoy it here and london too')
X_vectorized = …Run Code Online (Sandbox Code Playgroud) 如何使用Doc2vec获取两个文本文档的文档向量?我是新手,所以如果有人能指出我正确的方向/帮助我一些教程会很有帮助
我正在使用gensim.
doc1=["This is a sentence","This is another sentence"]
documents1=[doc.strip().split(" ") for doc in doc1 ]
model = doc2vec.Doc2Vec(documents1, size = 100, window = 300, min_count = 10, workers=4)
Run Code Online (Sandbox Code Playgroud)
我明白了
AttributeError:'list'对象没有属性'words'
每当我跑这个.
这些是我的python代码:
import subprocess
subprocess.check_output("ls",shell=True,stderr=subprocess.STDOUT)
import subprocess
subprocess.check_output("yum",shell=True,stderr=subprocess.STDOUT)
Run Code Online (Sandbox Code Playgroud)
第一次工作顺利,但第二次回归:
Traceback (most recent call last):
File "/usr/lib/x86_64-linux-gnu/gedit/plugins/pythonconsole/console.py", line 378, in __run
r = eval(command, self.namespace, self.namespace)
File "<string>", line 1, in <module>
File "/usr/lib/python3.4/subprocess.py", line 616, in check_output
raise CalledProcessError(retcode, process.args, output=output)
subprocess.CalledProcessError: Command 'yum' returned non-zero exit status 1
Run Code Online (Sandbox Code Playgroud)
为什么会这样?是因为ls是原始shell命令,但是yum是新包吗?如何解决这个问题呢?
我对熊猫很新,我试图连接一组数据帧,我收到这个错误:
ValueError: Plan shapes are not aligned
Run Code Online (Sandbox Code Playgroud)
我的理解.concat()是它会在列相同的地方加入,但对于那些找不到它的人来说,它将填充NA.这似乎不是这种情况.
继承人的声明:
dfs = [npo_jun_df, npo_jul_df,npo_may_df,npo_apr_df,npo_feb_df]
alpha = pd.concat(dfs)
Run Code Online (Sandbox Code Playgroud) python ×9
pandas ×4
dataframe ×2
scikit-learn ×2
concat ×1
gensim ×1
python-3.x ×1
rabbitmq ×1
rows ×1
subprocess ×1
word2vec ×1