Questions tagged «python»

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


9
如何使用pip要求文件来卸载和安装软件包?
我有一个pip需求文件,该文件在开发过程中会发生变化。 可以pip是由卸载,不会出现在需求文件以及安装那些确实出现包?有没有标准的方法? 这将使pip需求文件成为软件包的规范列表-一种“仅当且仅当”的方法。 更新:我在https://github.com/pypa/pip/issues/716上将其建议为一项新功能
89 python  pip 

8
如何在Python的matplotlib中设置“后端”?
我是matplotlib的新用户,我的平台是Ubuntu 10.04 Python 2.6.5 这是我的代码 import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt plt.plot([1,2,3]) 错误是: /usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py:41: UserWarning: Your currently selected backend, 'agg' does not support show(). Please select a GUI backend in your matplotlibrc file ('/usr/local/lib/python2.6/dist-packages/matplotlib/mpl-data/matplotlibrc') or with matplotlib.use() (backend, matplotlib.matplotlib_fname())) 我安装了“防颗粒几何”库,apt-get install libagg但是它不起作用。 我试图使用后端的其他参数,例如“ GTK”和“ TkAgg”。 我安装了python-gtk2-dev软件包,但错误仍在下面。 谁能告诉我一个可执行的后端参数及其依赖库? 这是错误: >>> …

10
Lambda函数的范围及其参数?
我需要一个与一系列gui事件几乎完全相同的回调函数。该函数的行为会有所不同,具体取决于调用该事件的事件。对我来说,这似乎是一个简单的案例,但是我无法弄清楚lambda函数的这种奇怪行为。 因此,我在下面有以下简化代码: def callback(msg): print msg #creating a list of function handles with an iterator funcList=[] for m in ('do', 're', 'mi'): funcList.append(lambda: callback(m)) for f in funcList: f() #create one at a time funcList=[] funcList.append(lambda: callback('do')) funcList.append(lambda: callback('re')) funcList.append(lambda: callback('mi')) for f in funcList: f() 此代码的输出是: mi mi mi …

9
Pycharm和sys.argv参数
我正在尝试调试将命令行参数作为输入的脚本。参数是同一目录中的文本文件。脚本从sys.argv列表中获取文件名。我的问题是我无法使用pycharm中的参数启动脚本。 我试图在“运行”>“编辑配置”菜单的“脚本参数”字段中输入参数,如下所示: -s'file1.txt', -s'file2.txt' 但这没有用。如何使用参数启动脚本? PS我在Ubuntu上
89 python  linux  pycharm 

3
在Pandas条形图上用值注释条
我一直在寻找一种方法,用DataFrame中的四舍五入数值在Pandas条形图中标注条形。 >>> df=pd.DataFrame({'A':np.random.rand(2),'B':np.random.rand(2)},index=['value1','value2'] ) >>> df A B value1 0.440922 0.911800 value2 0.588242 0.797366 我想得到这样的东西: 我尝试使用此代码示例,但所有注释均以x刻度为中心: >>> ax = df.plot(kind='bar') >>> for idx, label in enumerate(list(df.index)): for acc in df.columns: value = np.round(df.ix[idx][acc],decimals=2) ax.annotate(value, (idx, value), xytext=(0, 15), textcoords='offset points')

2
将DataFrames列表保存到多页Excel电子表格
如何将DataFrames列表导出到一个Excel电子表格中?状态 文档to_excel: 注意 如果传递现有的ExcelWriter对象,则工作表将添加到现有的工作簿中。这可用于将不同的DataFrame保存到一个工作簿中 writer = ExcelWriter('output.xlsx') df1.to_excel(writer, 'sheet1') df2.to_excel(writer, 'sheet2') writer.save() 之后,我想我可以编写一个将DataFrame列表保存到一个电子表格的函数,如下所示: from openpyxl.writer.excel import ExcelWriter def save_xls(list_dfs, xls_path): writer = ExcelWriter(xls_path) for n, df in enumerate(list_dfs): df.to_excel(writer,'sheet%s' % n) writer.save() 但是(带有两个小DataFrame的列表,每个小DataFrame可以to_excel单独保存),引发了一个异常(编辑:删除了traceback): AttributeError: 'str' object has no attribute 'worksheets' 大概我ExcelWriter打错电话了,应该怎么做才能做到这一点?

2
在matplotlib imshow()图形轴上更改值
说我有一些输入数据: data = np.random.normal(loc=100,scale=10,size=(500,1,32)) hist = np.ones((32,20)) # initialise hist for z in range(32): hist[z],edges = np.histogram(data[:,0,z],bins=np.arange(80,122,2)) 我可以使用绘制它imshow(): plt.imshow(hist,cmap='Reds') 得到: 但是,x轴值与输入数据不匹配(即平均值为100,范围从80到122)。因此,我想更改x轴以显示中的值edges。 我努力了: ax = plt.gca() ax.set_xlabel([80,122]) # range of values in edges ... # this shifts the plot so that nothing is visible 和 ax.set_xticklabels(edges) ... # this labels the …


7
Python,具有基本身份验证的HTTPS GET
我正在尝试使用python使用基本身份验证进行HTTPS GET。我对python非常陌生,这些指南似乎使用不同的库来做事。(http.client,httplib和urllib)。谁能告诉我它是如何完成的?您如何告诉标准库使用?

10
如何从列表Python / NumPy中删除Nan
我有一个列出值的列表,我得到的值之一是“ nan” countries= [nan, 'USA', 'UK', 'France'] 我试图将其删除,但每次都会收到错误消息 cleanedList = [x for x in countries if (math.isnan(x) == True)] TypeError: a float is required 当我尝试这个: cleanedList = cities[np.logical_not(np.isnan(countries))] cleanedList = cities[~np.isnan(countries)] TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any …
89 python  numpy 

13
比较两个数据框并获得差异
我有两个数据框。例子: df1: Date Fruit Num Color 2013-11-24 Banana 22.1 Yellow 2013-11-24 Orange 8.6 Orange 2013-11-24 Apple 7.6 Green 2013-11-24 Celery 10.2 Green df2: Date Fruit Num Color 2013-11-24 Banana 22.1 Yellow 2013-11-24 Orange 8.6 Orange 2013-11-24 Apple 7.6 Green 2013-11-24 Celery 10.2 Green 2013-11-25 Apple 22.1 Red 2013-11-25 Orange 8.6 …


7
如何在Python中读取一行csv数据?
有很多使用python读取csv数据的示例,例如: import csv with open('some.csv', newline='') as f: reader = csv.reader(f) for row in reader: print(row) 我只想读取一行数据并将其输入到各种变量中。我怎么做?我到处都在寻找一个可行的例子。 我的代码仅检索i的值,而没有其他值 reader = csv.reader(csvfile, delimiter=',', quotechar='"') for row in reader: i = int(row[0]) a1 = int(row[1]) b1 = int(row[2]) c1 = int(row[2]) x1 = int(row[2]) y1 = int(row[2]) z1 = int(row[2])
89 python  file  csv  iterator  next 


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.