Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

5
将包含多行JSON的文件加载到Pandas中
我正在尝试将JSON文件读入Python熊猫(0.14.0)数据帧中。这是JSON文件的第一行: {"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "P_Mk0ygOilLJo4_WEvabAA", "review_id": "OeT5kgUOe3vcN7H6ImVmZQ", "stars": 3, "date": "2005-08-26", "text": "This is a pretty typical cafe. The sandwiches and wraps are good but a little overpriced and the food items are the same. The chicken caesar salad wrap is my favorite here but …


7
pip抛出TypeError:尝试安装新软件包时parse()得到了意外的关键字参数'transport_encoding'
我正在使用最新版本的Anaconda3。我刚刚安装了它,我正在尝试下载一些软件包。我正在使用Anaconda Prompt。尝试使用pip做任何事情(包括升级现有软件包)时,我得到以下回溯。 Exception: Traceback (most recent call last): File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main status = self.run(options, args) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run wb.build(autobuilding=True) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build self.requirement_set.prepare_files(self.finder) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files ignore_dependencies=self.ignore_dependencies)) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\req\req_set.py", line 487, in _prepare_file req_to_install, finder) File "C:\Users\csprock\Anaconda3\lib\site-packages\pip\req\req_set.py", line …

10
Selenium:FirefoxProfile异常无法加载配置文件
对于这个先前的问题,我将Selenium更新到了2.0.1版本,但是现在我又遇到了另一个错误,即使配置文件位于以下位置/tmp/webdriver-py-profilecopy: 在执行中,文件“ /home/sultan/Repository/Django/monitor/app/request.py”,第236行 浏览器= Firefox(配置文件) __init__中的文件“ /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py”,第46行 self.binary,超时), __init__中的文件“ /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py”,第46行 self.binary.launch_browser(self.profile) 在launch_browser中的第44行,文件“ /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py” self._wait_until_connectable() _wait_until_connectable中的文件“ /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py”,第87行 引发WebDriverException(“无法加载配置文件。配置文件目录:%s”%self.profile.path) selenium.common.exceptions.WebDriverException:无法加载配置文件。个人档案目录:/ tmp / webdriver-py-profilecopy 怎么了?我该如何解决这个问题?


17
找不到资源u'tokenizers / punkt / english.pickle'
我的代码: import nltk.data tokenizer = nltk.data.load('nltk:tokenizers/punkt/english.pickle') 错误信息: [ec2-user@ip-172-31-31-31 sentiment]$ python mapper_local_v1.0.py Traceback (most recent call last): File "mapper_local_v1.0.py", line 16, in <module> tokenizer = nltk.data.load('nltk:tokenizers/punkt/english.pickle') File "/usr/lib/python2.6/site-packages/nltk/data.py", line 774, in load opened_resource = _open(resource_url) File "/usr/lib/python2.6/site-packages/nltk/data.py", line 888, in _open return find(path_, path + ['']).open() File "/usr/lib/python2.6/site-packages/nltk/data.py", line 618, in …
96 python  unix  nltk 

5
如何在Python中使用子进程重定向输出?
我在命令行中执行的操作: cat file1 file2 file3 > myfile 我想用python做什么: import subprocess, shlex my_cmd = 'cat file1 file2 file3 > myfile' args = shlex.split(my_cmd) subprocess.call(args) # spits the output in the window i call my python program

6
如何在TensorFlow中应用梯度裁剪?
考虑示例代码。 我想知道如何在RNN上的该网络上应用梯度剪切,而梯度可能会爆炸。 tf.clip_by_value(t, clip_value_min, clip_value_max, name=None) 这是一个可以使用的示例,但是在哪里介绍呢?在RNN中 lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0) # Split data because rnn cell needs a list of inputs for the RNN inner loop _X = tf.split(0, n_steps, _X) # n_steps tf.clip_by_value(_X, -1, 1, name=None) 但这没有意义,因为张量_X是输入,而不是grad,要裁剪的内容是什么? 我是否需要为此定义自己的优化器,还是有一个更简单的选择?


6
“ x不在y”或“ x不在y”
在测试成员资格时,我们可以使用: x not in y 或者: not x in y 取决于x和,此表达式可能有许多可能的上下文y。例如,它可能用于子字符串检查,列表成员资格,字典键存在。 两种形式总是相等的吗? 有首选的语法吗?

4
如何使用点绘制熊猫数据框的两列?
我有一个pandas数据框,想绘制一列的值与另一列的值。幸运的是,有plot一种与数据帧相关的方法似乎可以满足我的需求: df.plot(x='col_name_1', y='col_name_2') 不幸的是,它看起来像打印样式(上市中这里后kind参数)有没有点。我可以使用线或条,甚至可以使用密度,但不能使用点。是否有解决方法可以帮助解决此问题。

4
DataFrame中的字符串,但dtype是object
为什么Pandas告诉我我有对象,尽管所选列中的每个项目都是一个字符串-即使经过显式转换也是如此。 这是我的DataFrame: <class 'pandas.core.frame.DataFrame'> Int64Index: 56992 entries, 0 to 56991 Data columns (total 7 columns): id 56992 non-null values attr1 56992 non-null values attr2 56992 non-null values attr3 56992 non-null values attr4 56992 non-null values attr5 56992 non-null values attr6 56992 non-null values dtypes: int64(2), object(5) 他们五个dtype object。我将这些对象明确转换为字符串: for c …
96 python  pandas  numpy  types  series 

4
在try-except块中使用python“ with”语句
这是将python“ with”语句与try-except块结合使用的正确方法吗? try: with open("file", "r") as f: line = f.readline() except IOError: <whatever> 如果是这样,请考虑使用旧的处理方式: try: f = open("file", "r") line = f.readline() except IOError: <whatever> finally: f.close() 这里的“ with”语句的主要好处是我们可以摆脱三行代码吗?对于这个用例,这似乎并不吸引我(尽管我知道“ with”语句还有其他用途)。 编辑:以上两个代码块的功能是否相同? EDIT2:前几个答案大体上谈及使用“ with”的好处,但是这里似乎没有什么好处。我们已经(或者应该已经)明确地调用f.close()多年了。我想一个好处是草率的编码器将从使用“ with”中受益。

5
如何使Pandas DataFrame列标题全部小写?
我想使我的pandas数据框中的所有列标题都小写 例 如果我有: data = country country isocode year XRAT tcgdp 0 Canada CAN 2001 1.54876 924909.44207 1 Canada CAN 2002 1.56932 957299.91586 2 Canada CAN 2003 1.40105 1016902.00180 .... 我想通过执行以下操作将XRAT更改为xrat: data.headers.lowercase() 这样我得到: country country isocode year xrat tcgdp 0 Canada CAN 2001 1.54876 924909.44207 1 Canada CAN 2002 1.56932 …


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.