给定一个带有"BoolCol"列的DataFrame,我们想要找到DataFrame的索引,其中"BoolCol"的值== True
我目前有迭代的方式来做到这一点,它完美地工作:
for i in range(100,3000):
if df.iloc[i]['BoolCol']== True:
print i,df.iloc[i]['BoolCol']
Run Code Online (Sandbox Code Playgroud)
但这不是正确的熊猫方式.经过一些研究,我目前正在使用此代码:
df[df['BoolCol'] == True].index.tolist()
Run Code Online (Sandbox Code Playgroud)
这个给了我一个索引列表,但是当我通过执行以下操作检查它们时它们不匹配:
df.iloc[i]['BoolCol']
Run Code Online (Sandbox Code Playgroud)
结果实际上是假的!!
这是正确的熊猫方式吗?
对于大量嵌套字典,我想检查它们是否包含密钥.它们中的每一个都可能有也可能没有嵌套字典之一,所以如果我循环搜索所有这些字典会引发错误:
for Dict1 in DictionariesList:
if "Dict4" in Dict1['Dict2']['Dict3']:
print "Yes"
Run Code Online (Sandbox Code Playgroud)
我目前的解决方案是:
for Dict1 in DictionariesList:
if "Dict2" in Dict1:
if "Dict3" in Dict1['Dict2']:
if "Dict4" in Dict1['Dict2']['Dict3']:
print "Yes"
Run Code Online (Sandbox Code Playgroud)
但这是令人头痛的,丑陋的,可能不是非常有效的资源.这是以第一种方式执行此操作的正确方法,但在字典不存在时不会引发错误?
假设我们有这个代码:
a = 1
def func1():
if a == 1:
func2()
def func2():
if a == 1:
func3()
def func3():
func1()
Run Code Online (Sandbox Code Playgroud)
有没有办法让 func3 call func1跳出它已经产生的“父函数”?意思是,回到“递归深度 0”,就好像它重新开始一样?
谢谢!
我想用dinamically调用一个对象方法.
变量"MethodWanted"包含我想要执行的方法,变量"ObjectToApply"包含该对象.到目前为止我的代码是:
MethodWanted=".children()"
print eval(str(ObjectToApply)+MethodWanted)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
exception executing script
File "<string>", line 1
<pos 164243664 childIndex: 6 lvl: 5>.children()
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我也试过没有str()包装对象,但后来我得到了"无法使用+与str和对象类型"错误.
当没有动态时,我可以这样做:
ObjectToApply.children()
Run Code Online (Sandbox Code Playgroud)
我得到了理想的结果.
怎么做dinamically?
我正在构建一个多用途的用户界面,我正在为它添加Pandas.为此,我需要按用户选择定义的组件(存储在变量中)形成表达式.
一切似乎都很好,但我陷入了死胡同.我希望用户能够选择几个表达式,然后将它们连接起来以形成新的数据帧.如果我只使用一个表达式,一切都会起作用:
from pandas import read_csv
df = read_csv("SomeCsv.csv")
b= df[r'ID']
a=(b==r'p')
Value=df[a] #Works,returning the rows in df whichs column 'ID' equals r'p'
Run Code Online (Sandbox Code Playgroud)
但是如果我想要包含更多表达式:
from pandas import read_csv
df = read_csv("SomeCsv.csv")
b= df[r'ID']
c=(b==r'p')
d=(b==r'ul')
a=c or d #Breaks at this line
Value=df[a] #Doesnt work. I would expect the rows in df whichs column 'ID' equals r'p' or 'ID' equals r'ul'
Run Code Online (Sandbox Code Playgroud)
并抛出以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() …Run Code Online (Sandbox Code Playgroud) 我是第一次使用这个库所以我不确定这是一个错误还是我没有正确地做某事.
我想将文件导出为mp3,加载完美:
wav=AudioSegment.from_wav(Path) #If I execute only this line, there are no errors.
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将我的文件导出到mp3时:
wav.export(r"WavOut.mp3",format="mp3")
Run Code Online (Sandbox Code Playgroud)
它会引发以下错误:
WindowsError:[错误2]系统找不到指定的文件
完整的错误报告:http://pastebin.com/3CpZBkEK
我相信我使用的语法正确.它创建了mp3输出文件,但绝对是空的,并且输入路径是正确的,因为:
os.startfile(Path) #Will work
Run Code Online (Sandbox Code Playgroud)
另外,我在windows路径中注册了ffmpeg:
;c:\ffmpeg\bin #It is currently callable from any cmd prompt in windows.
Run Code Online (Sandbox Code Playgroud)
什么可以失败?谢谢!
经过更多测试,我发现了它的编码问题.如果我将输出设置为"wav"就可以了.但我真的不知道我该怎样安装一个MP3编码器为它与pydub工作,所以我改变了问题要问的是,因为它的真正的问题.安装ffmpeg所以我不知道还能做什么:
我刚刚安装了 tampermonkey 和“hello world”脚本,但它没有运行。我没有发现任何关于如何进行的迹象。其余的脚本似乎都没有工作。我应该尝试什么?
(这个问题主要针对创建者,名为derjanb的stackoverflow用户)
谢谢你。
编辑:在downvote之后编辑:我在这里问之前做了研究。但是没有办法联系到作者,所以我试试这个。我在greasemonkey google group、他的论坛中搜索了解决方案,并试图找到他的联系电子邮件,但没有成功。然后在 Stackoverflow 上找到了一些 Tampermonkey 问题和答案,并决定在这里发布。关于我的问题的信息很少,但实际上,我发现没有任何关于脚本根本没有运行的故障排除。
我将解释这个例子,因为它更容易描述这种方式:
假设我们有一个未确定的(X)变量数,可以是列表或字符串.
例如,X = 3,我们有:
Var1=list("a","b")
Var2=list("c","d")
Var3="e"
Run Code Online (Sandbox Code Playgroud)
所有这些都在一个列表中:ListOfVariables = [Var1,Var2,Var3]
然后我们在这些变量上运行一个函数(我们事先不知道这个函数,但它使用的是我们拥有的相同数量X的变量).
def Function(Var1,Var2,Var3)
print Var1
print Var2
print Var3
Run Code Online (Sandbox Code Playgroud)
这里的目标是使函数与所有输入变量一起运行,如果其中一个是列表,它必须为列表中的每个项执行Function.所以想要的结果就是为所有这些调用Function,如下所示:
Function(a,c,e)
Function(a,d,e)
Function(b,c,e)
Function(b,d,e)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我使用一个辅助函数来识别Var1,Var2和Var3,但是我的大脑没有那么多的递归可预测性,因为能够定义这个HelperFunction,例如:
def HelperFunction():
for item in ListOfVariables:
if type(item).__name__=='list':
#This one will be done as a "for", so all the items in this list are executed as input (Var1 and Var2 in the example)
else:
#This item doesnt need to be included in a for, just execute once (Var3 in the example)
Run Code Online (Sandbox Code Playgroud)
我知道它可以用python完成,但我不能在脑海中编写我需要的功能,它比我的"大脑模拟器"更复杂一度可以模拟python =(
非常感谢您的帮助. …
python ×6
nested ×3
function ×2
pandas ×2
variables ×2
call ×1
depth ×1
dictionary ×1
dynamic ×1
export ×1
expression ×1
ffmpeg ×1
filter ×1
indexing ×1
methods ×1
mp3 ×1
pydub ×1
python-2.7 ×1
recursion ×1
tampermonkey ×1