三重行情
'''
This is a
multi-line
string.
'''
Run Code Online (Sandbox Code Playgroud)
串联
('this is '
'a string')
Run Code Online (Sandbox Code Playgroud)
逃跑
'This is'\
'a string'
Run Code Online (Sandbox Code Playgroud)
我也知道在字符串前面加上前缀r
会使它成为原始字符串,对文件路径很有用。
r'C:\Path\To\File'
Run Code Online (Sandbox Code Playgroud)但是,我有一个很长的文件路径,它跨越多行并且需要是一个原始字符串。我该怎么做呢?
这有效:
In [1]: (r'a\b'
...: '\c\d')
Out[1]: 'a\\b\\c\\d'
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,这不会:
In [4]: (r'on\e'
...: '\tw\o')
Out[4]: 'on\\e\tw\\o'
Run Code Online (Sandbox Code Playgroud)
为什么"t"
只有一个反斜杠?
熊猫数据框:
构造函数:
c = pd.MultiIndex.from_product([['AAPL','AMZN'],['price','custom']])
i = pd.date_range(start='2017-01-01',end='2017-01-6')
df1 = pd.DataFrame(index=i,columns=c)
df1.loc[:,('AAPL','price')] = list(range(51,57))
df1.loc[:,('AMZN','price')] = list(range(101,107))
df1.loc[:,('AAPL','custom')] = list(range(1,7))
df1.loc[:,('AMZN','custom')] = list(range(17,23))
df1.index.set_names('Dates',inplace=True)
df1.sort_index(axis=1,level=0,inplace=True) # needed for pd.IndexSlice[]
df1
Run Code Online (Sandbox Code Playgroud)
产生:(不知道如何格式化 Jupyter Notebook 的输出)
AAPL AMZN
custom price custom price
Dates
2017-01-01 1 51 17 101
2017-01-02 2 52 18 102
2017-01-03 3 53 19 103
2017-01-04 4 54 20 104
2017-01-05 5 55 21 105
2017-01-06 6 56 22 106
Run Code Online (Sandbox Code Playgroud)
问:
我怎样才能在多指标的第二个级别的区别是创建一个第三列price
和custom
?这应该针对每个顶部列级别单独计算,即分别针对 AAPL 和 AMZN。 …
import queue
qq = queue.Queue()
qq.put('hi')
class MyApp():
def __init__(self, q):
self._queue = q
def _process_item(self, item):
print(f'Processing this item: {item}')
def get_item(self):
try:
item = self._queue.get_nowait()
self._process_item(item)
except queue.Empty:
pass
async def listen_for_orders(self):
'''
Asynchronously check the orders queue for new incoming orders
'''
while True:
self.get_item()
await asyncio.sleep(0)
a = MyApp(qq)
loop = asyncio.get_event_loop()
loop.run_until_complete(a.listen_for_orders())
Run Code Online (Sandbox Code Playgroud)
使用 Python 3.6。
我正在尝试编写一个事件处理程序,它不断地侦听 中的消息queue
并处理它们(在这种情况下打印它们)。但它必须是异步的——我需要能够在终端 (IPython) 中运行它并手动将内容提供给queue
(至少在最初,用于测试)。
此代码不起作用 - 它永远阻塞。
如何让这个永远运行但在while
循环的每次迭代后返回控制?
谢谢。
旁注: 为了使事件循环与 IPython(7.2 …
我正在尝试使用concat
, 在它们的日期时间索引上合并 2 个数据帧,但它没有按我预期的那样工作。我从这个例子的文档中的例子中复制了一些代码:
import pandas as pd
df = pd.DataFrame({'year': [2015, 2016],
'month': [2, 3],
'day': [4, 5],
'value': [444,555]})
df.set_index(pd.to_datetime(df.loc[:,['year','month','day']]),inplace=True)
df.drop(['year','month','day'],axis=1,inplace=True)
df2 = pd.DataFrame(data=[222,333],
index=pd.to_datetime(['2015-02-04','2016-03-05']))
pd.concat([df,df2])
Out[1]:
value 0
2015-02-04 444.0 NaN
2016-03-05 555.0 NaN
2015-02-04 NaN 222.0
2016-03-05 NaN 333.0
Run Code Online (Sandbox Code Playgroud)
为什么它不能识别索引上的相同日期并相应地合并?我验证了两个索引都是 DateTime:
df.index
Out[2]: DatetimeIndex(['2015-02-04', '2016-03-05'], dtype='datetime64[ns]', freq=None)
df2.index
Out[3]: DatetimeIndex(['2015-02-04', '2016-03-05'], dtype='datetime64[ns]', freq=None)
Run Code Online (Sandbox Code Playgroud)
谢谢。
python ×4
pandas ×2
datetime ×1
multi-index ×1
multiline ×1
python-3.x ×1
rawstring ×1
string ×1