Questions tagged «python»

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

5
Python Pandas如何将groupby操作结果分配回父数据帧中的列?
我在IPython中具有以下数据框,其中每一行都是一只股票: In [261]: bdata Out[261]: <class 'pandas.core.frame.DataFrame'> Int64Index: 21210 entries, 0 to 21209 Data columns: BloombergTicker 21206 non-null values Company 21210 non-null values Country 21210 non-null values MarketCap 21210 non-null values PriceReturn 21210 non-null values SEDOL 21210 non-null values yearmonth 21210 non-null values dtypes: float64(2), int64(1), object(4) 我想应用一个groupby操作,计算“ yearmonth”列中每个日期的所有内容的上限加权平均回报。 这按预期工作: …

3
从(row,col,values)元组列表构造pandas DataFrame
我有一个元组列表,例如 data = [ ('r1', 'c1', avg11, stdev11), ('r1', 'c2', avg12, stdev12), ('r2', 'c1', avg21, stdev21), ('r2', 'c2', avg22, stdev22) ] 我想将它们放入一个熊猫数据框,其中第一行命名为行,第二列命名为列。看来,处理行名称的方法类似,pandas.DataFrame([x[1:] for x in data], index = [x[0] for x in data])但如何处理列以获得2x2矩阵(前一组的输出为3x4)?是否还有一种更智能的方式来处理行标签,而不是显式地忽略它们? 编辑似乎我将需要2个数据框-一个用于平均值,一个用于标准差,对吗?还是可以在每个“单元格”中存储值列表?

2
熊猫to_html()截断字符串内容
我有一个DataFrame包含文本数据的Python Pandas对象。我的问题是,当我使用to_html()函数时,它会截断输出中的字符串。 例如: import pandas df = pandas.DataFrame({'text': ['Lorem ipsum dolor sit amet, consectetur adipiscing elit.']}) print (df.to_html()) 输出在处被截断 adapis... <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>text</th> </tr> </thead> <tbody> <tr> <th>0</th> <td> Lorem ipsum dolor sit amet, consectetur adipis...</td> </tr> </tbody> </table> 关于SO有一个相关问题,但是它使用占位符和搜索/替换功能对HTML进行后处理,我想避免这种情况: 将Pandas数据框的全部内容写入HTML表 有没有更简单的解决方案来解决这个问题?我从文档中找不到任何相关内容。
81 python  html  pandas 

12
Python-如何检查列表的单调性
什么是检查列表单调性的有效且Python方式?即它具有单调增加或减少的值? 例子: [0, 1, 2, 3, 3, 4] # This is a monotonically increasing list [4.3, 4.2, 4.2, -2] # This is a monotonically decreasing list [2, 3, 1] # This is neither

10
使用Python列出可用的com端口
我正在寻找一种列出PC上所有可用com端口的简单方法。 我找到了这种方法,但是它是Windows特定的:在Windows上列出串行(COM)端口? 我在Windows 7 PC上使用带有pySerial的Python 3。 我在pySerial API(http://pyserial.sourceforge.net/pyserial_api.html)中找到了serial.tools.list_ports.comports()列出com端口(正是我想要的功能)的函数。 import serial.tools.list_ports print(list(serial.tools.list_ports.comports())) 但似乎不起作用。当我的USB到COM网关连接到PC时(我在设备管理器中看到了COM5),该COM端口未包含在所返回的列表中list_ports.comports()。相反,我只能得到似乎已连接到调制解调器的COM4(我在“设备管理器”的“ COM&LPT”部分中没有看到它)! 您知道为什么它不起作用吗?您有不是系统特定的另一种解决方案吗?
81 python  pyserial 

7
Python:鉴于原始电子邮件没有“ Body”标签或任何内容,因此如何从原始电子邮件中解析正文
看起来很容易 From To Subject 等通过 import email b = email.message_from_string(a) bbb = b['from'] ccc = b['to'] 假设这"a"是原始电子邮件字符串,看起来像这样。 a = """From root@a1.local.tld Thu Jul 25 19:28:59 2013 Received: from a1.local.tld (localhost [127.0.0.1]) by a1.local.tld (8.14.4/8.14.4) with ESMTP id r6Q2SxeQ003866 for <ooo@a1.local.tld>; Thu, 25 Jul 2013 19:28:59 -0700 Received: (from root@localhost) by …

1
什么时候需要使用sqlalchemy back_populates?
当我尝试按照本指南进行SQLAlchemy Relation Example时:基本关系模式 我有这个代码 #!/usr/bin/env python # encoding: utf-8 from sqlalchemy import create_engine from sqlalchemy import Table, Column, Integer, ForeignKey from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.declarative import declarative_base engine = create_engine('sqlite:///:memory:', echo=True) Session = sessionmaker(bind=engine) session = Session() Base = declarative_base(bind=engine) class Parent(Base): __tablename__ = 'parent' id = Column(Integer, …

4
matplotlib y轴标签在右侧
是否有一种简单的方法可以将y轴标签放在图的右侧?我知道可以使用来对刻度标签进行此操作ax.yaxis.tick_right(),但是我想知道是否也可以对轴标签进行此操作。 我想到的一个想法是使用 ax.yaxis.tick_right() ax2 = ax.twinx() ax2.set_ylabel('foo') 但是,在保留y轴的范围的同时,将所有标签(刻度和轴标签)放在右侧并没有达到预期的效果。简而言之,我想要一种将所有y轴标签从左向右移动的方法。


9
为什么会出现AttributeError:对象没有属性[关闭]
关闭。这个问题不能重现,或者是由错别字引起的。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow的主题。 10个月前关闭。 改善这个问题 我有一个类MyThread。在那我有一个方法示例。我试图从相同的对象上下文中运行它。请看一下代码: class myThread (threading.Thread): def __init__(self, threadID, name, counter, redisOpsObj): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.redisOpsObj = redisOpsObj def stop(self): self.kill_received = True def sample(self): print "Hello" def run(self): time.sleep(0.1) print "\n Starting " + self.name self.sample() 看起来很简单,不是吗。但是当我运行它时,我得到这个错误 AttributeError: 'myThread' …
81 python 


11
如何使用python argparse解析多个嵌套的子命令?
我正在实现一个命令行程序,其界面如下: cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} ...] 我已经阅读了argparse文档。我可以GLOBAL_OPTIONS使用add_argumentin实现为可选参数argparse。以及{command [COMMAND_OPTS]}using子命令。 从文档看来,我只能有一个子命令。但是如您所见,我必须实现一个或多个子命令。解析此类命令行参数使用的最佳方法是什么argparse?



4
sqlalchemy过滤多列
如何合并两列并应用过滤器?例如,我要同时搜索“名字”和“姓氏”列。如果仅搜索一列,这就是我的操作方式: query = meta.Session.query(User).filter(User.firstname.like(searchVar))

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.